您的位置:首页 > 编程语言 > Java开发

Java实现MD5

2016-12-20 21:59 190 查看
本文摘自:http://www.cnblogs.com/yanghuahui/archive/2013/06/16/3139411.html

经实践检测可行,以备以后使用。

import java.security.MessageDigest;

/**
* Created by Administrator on 2016/11/26.
*/
public class test {
public static void main(String[] args) {
MD5("test");
}

public static String MD5(String basicInfo){
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
try {
byte[] strTemp = basicInfo.getBytes();
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(strTemp);
byte[] md = md5.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: