md消息摘要加密,不可逆,128位,32位hex

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
public static String getMd5(String mingwen) {
StringBuffer sb = new StringBuffer();
try {
MessageDigest md = MessageDigest.getInstance(“md5”);
//转字符数组
char[] charArray=mingwen.toCharArray();
//字节数组
byte[] byteArray=new byte[charArray.length];
//转
for(int i=0;i byteArray[i]=(byte)charArray[i];
}
//加密
byte[] miwen=md.digest(byteArray);
//转十六进制
for(int i=0;i int val=(int)miwen[i]&0xff;//去掉负数
//补位
if(val<16)
sb.append(“0”);
sb.append(Integer.toHexString(val));
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args){
String result=MD5.getMd5(“123456”);
System.out.println(result);
}
}

你可能感兴趣的:(md5,java,密码学)