MD5加密算法

public static String toMD5str(String str)

  String md5string="";

  try

  MessageDigest algorithm =MessageDigest.getInstance("MD5");

  algorithm.reset();

 algorithm.update(str.getBytes("utf-8"));

md5string=toHexString(algorithm.digest());

catch(NoSuchAlgorithmException e)

    throw new RuntimeException

catch(UnsupportEncodingException e)

e.printStackTrace();

return md5string;

 

 

public static String toHexString(byte[] bytes)

StringBuilder hexString=new StringBuilder();

for (int b : bytes) {
   if (b < 0){
    b += 256;
   }
   if (b < 16){
    hexString.append("0");    
   }
   hexString.append(Integer.toHexString(b));
  }
  return hexString.toString();

 

 

 

 

本文出自 “蒲公英的梦想” 博客,转载请与作者联系!

你可能感兴趣的:(MD5加密算法)