import java.security.*;
import java.security.spec.*;
public class MD5 {
public final static String compile(String s) {
char[] hex = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
byte[] tmp = s.getBytes();
try {
MessageDigest dig = MessageDigest.getInstance("MD5");
dig.update(tmp);
byte[] md = dig.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hex[byte0 >>> 4 & 0xf];
str[k++] = hex[byte0 & 0xf];
}
return new String(str);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
return null;
}
}
public static void main(String[] args){
System.out.println(MD5.compile("aa"));
}
/** Creates a new instance of MD5 */
private MD5() {
}
}
import java.security.spec.*;
public class MD5 {
public final static String compile(String s) {
char[] hex = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
byte[] tmp = s.getBytes();
try {
MessageDigest dig = MessageDigest.getInstance("MD5");
dig.update(tmp);
byte[] md = dig.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hex[byte0 >>> 4 & 0xf];
str[k++] = hex[byte0 & 0xf];
}
return new String(str);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
return null;
}
}
public static void main(String[] args){
System.out.println(MD5.compile("aa"));
}
/** Creates a new instance of MD5 */
private MD5() {
}
}
转载于:https://blog.51cto.com/evan2008/132871