当前位置:首页 > Android

android环境下两种md5加密方式

jsc9年前 (2016-09-19)Android4148

在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来。虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md5的认知目前仅仅停留在会使用的水平,想搞清楚还是要花点时间的,这是md5加密算法的 dex" style="color: rgb(148, 148, 148); text-decoration: none; transition: 0.25s; outline: none 0px; border-bottom: 1px dashed rgb(148, 148, 148); font-style: italic; font-weight: bold;">相关介绍 。 本文主要介绍android平台下两种md5加密方式,分别为基于java语言的md5加密及ndk环境下基于c语言的md5加密。

下面代码为基于java语言的md5加密:

public String getMD5(String info)  
{  
    try  
    {  
        MessageDigest md5 = MessageDigest.getInstance("MD5");  
        md5.update(info.getBytes("UTF-8"));  
        byte[] encryption = md5.digest();  
              
        StringBuffer strBuf = new StringBuffer();  
        for (int i = 0; i < encryption.length; i++)  
        {  
            if (Integer.toHexString(0xff & encryption[i]).length() == 1)  
            {  
                strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));  
            }  
            else  
            {  
                strBuf.append(Integer.toHexString(0xff & encryption[i]));  
            }  
        }  
              
        return strBuf.toString();  
    }  
    catch (NoSuchAlgorithmException e)  
    {  
        return "";  
    }  
    catch (UnsupportedEncodingException e)  
    {  
        return "";  
    }  
}

下面代码为ndk环境下基于c语言的md5加密:

include <jni.h>  
#include <stdio.h>  
#include <string.h>  
#include "md5.h"  
  
// md5加密  
JNIEXPORT jstring JNICALL Java_com_example_testmd5_MainActivity_encryptByMD5(JNIEnv *env, jclass obj, jstring strText)  
{  
    char* szText = (char*)(*env)->GetStringUTFChars(env, strText, 0);  
  
    MD5_CTX context = { 0 };  
    MD5Init(&context);  
    MD5Update(&context, szText, strlen(szText));  
    unsigned char dest[16] = { 0 };  
    MD5Final(dest, &context);  
    (*env)->ReleaseStringUTFChars(env, strText, szText);  
  
    int i = 0;  
    char szMd5[32] = { 0 };  
    for (i = 0; i < 16; i++)  
    {  
        sprintf(szMd5, "%s%02x", szMd5, dest[i]);  
    }  
  
    return (*env)->NewStringUTF(env, szMd5);  
}

不过上述代码仅仅为其中一部分,因为md5加密算法的c源码文件较长,就不显示在这里了,想研究的同学可以下载完整工程查看,工程下载链接为:http://download.csdn.net/detail/u013085897/8097613。工程运行结果如下图所示,hello world为待加密的字符串。

2432.png

扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/126.html

标签: dex数据加密
分享给朋友:

“android环境下两种md5加密方式” 的相关文章

Android自定义属性,format详解

1. reference:参考某一资源ID。     (1)属性定义:             <declare-styleable name = &qu…

TypedArray和obtainStyledAttributes使用

TypedArray和obtainStyledAttributes使用

在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用......…

Android中Parcelable接口用法

1. Parcelable接口Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelabl…

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

˂p style="font-family:Arial; font-size:14px; line-height:26px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0…

下拉刷新及滚动到底部加载更多的Listview使用

下拉刷新及滚动到底部加载更多的Listview使用

本文主要介绍可同时实现下拉刷新及滑动到底部加载更多的ListView的使用。 该ListView优点包括:a. 可自定义下拉响应事件(如下拉刷新)  b.可自定义滚动到底部响应的事件(如滑动到底部加载更多)  c.可自定义丰富的样式  d.高效(若下拉…

修改keystore密码别名等

修改keystore密码别名等

之前在测试Eclipse ADT的Custom debug ˂a target="_blank" title="View all posts in keystore" hr…