在做系统时一般都会涉及到登陆注册问题,为了系统的安全性,我们就不得不采用加密算法来对密码进行加密,下面为大家简单介绍一下MD5加密算法的代码:
package com.test;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public static String getMD5String(String s){
char[] hex = "abcdef0123456789".toCharArray();
char[] rs = new char[hex.length*2];
try {
MessageDigest md5 = MessageDigest.getInstance("md5");
md5.update(s.getBytes());
byte[] data = md5.digest();
int k = 0;
for(int i=0;i<hex.length;i++){
rs[k++] = hex[data[i]>>>4&0xf];
rs[k++] = hex[data[i]&0xf];
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new String(rs);
}
}