package test;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class md5 {
// public md5(String newsource) {
// // TODO Auto-generated constructor stub
// }
public String md5(String source){
String dest = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
char[] charArray = source.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i=0; i<charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i=0; i<md5Bytes.length; i++)
{
int val = ((int) md5Bytes[i] ) & 0xff;
if (val < 16) hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
dest = hexValue.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return dest;
}
public static void main(String[] args){
String source = "nwt123456";
String newsource = "nwt"+source;
md5 md = new md5();
md.md5(source);
// md5 m1 = new md5(newsource);
md5 m2 = new md5();
m2.md5(newsource);
System.out.println(source+"---"+md.md5(source));
System.out.println(newsource+"---"+m2.md5(newsource));
}
}
实现MD5验证的类
最新推荐文章于 2022-09-05 20:06:18 发布