MD5

来源:互联网 发布:企业社交网络 编辑:程序博客网 时间:2024/05/22 03:28

Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。该算法的文件号为RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992)

下面是java的MD5实现:

import java.security.MessageDigest;
public class MD5 {
 public static String MD5(String inStr) {
  MessageDigest md5 = null;
  try {
   md5 = MessageDigest.getInstance("MD5");
  } catch (Exception e) {
   return "";
  }
  char[] charArray = inStr.toCharArray();
  byte[] byteArray = new byte[charArray.length];
  for (int i = 0; i < charArray.length; i++)
   byteArray[i] = (byte) charArray[i];
  byte[] md5Bytes = md5.digest(byteArray);
  StringBuffer hexValue = new StringBuffer();
  for (int i = 0; i < md5Bytes.length; i++) {
   int val = (md5Bytes[i]) & 0xff;
   if (val < 16)
    hexValue.append("0");
   hexValue.append(Integer.toHexString(val));
  }
  return hexValue.toString();
 }
}

原创粉丝点击