MD5 加密:
新建如下類,再需要的時候調用即可。
package com.fuying.assure.baseInfo.util;
import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
/**@author hongk 2015/06/17
* MD5加密
* */
public class Md5Util {
public static String md5Digest(String pwd){
try {
MessageDigest md5Code=
MessageDigest.getInstance("md5");
byte[] bytes=md5Code.digest(pwd.getBytes());
BASE64Encoder base64=new BASE64Encoder();
pwd=base64.encode(bytes);
return pwd;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getMD5(String pwd){
try {
String s=null;
char hexChar[] = { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
MessageDigest md=MessageDigest.getInstance("md5");
md.update(pwd.getBytes());
byte[] bytes=md.digest();
char result[] = new char[16*2];//返回32位字符串,每位是16進製表示
int k=0;
for(int i=0;i<16;i++){//字節轉換
byte eByte=bytes[i];
result[k++] = hexChar[eByte >>> 4 & 0xf];
result[k++] = hexChar[eByte & 0xf];
}
s=new String(result);
return s;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String str ="pass1234";
System.out.println(getMD5(str));
}
}