您的位置:首页 > 其它

MD5加密技术

2016-05-23 11:13 288 查看
直接上代码,如下:

public class MD5Demoe01 {
public static String hashKeyForDisk(String key) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}

private static String bytesToHexString(byte[] bytes) {
// http://stackoverflow.com/questions/332079 StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}

public static void main(String[] args) {
String key="123456";
key=hashKeyForDisk(key);
System.out.println(key);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: