`
xiaoshao
  • 浏览: 50319 次
社区版块
存档分类
最新评论

java MD5 加密

 
阅读更多

下面是一个简单的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);

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics