下面是一个简单的java MD5加密算法。
/** MD5 encrypt algorithm*/
public class MD5{
public static String encryptTo32(String input){
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] ciphertext = digest.digest(input.getBytes());
//convert 8 bytes cipher text to 16 bytes cipher text
int temp; StringBuilder builder = new StringBuilder(32);
for(int i = 0; i < ciphertext.length; i ++){
temp = ciphertext[i];
if(temp < 0){
temp += 256;
}
if(temp<16){
builder.append("0");
}
builder.append(Integer.toHexString(temp));
}
return builder.toString();
}
public static String encryptTo16(String input){
encryptTo32(input).subString(8,24);
}
}