1、MD5算法的实现步骤
(1)把加密的字符串转换成字节数组。
(2)获取MessageDigest对象,利用该对象的digest方法完成加密,返回字节数组。
(3)将字节数组利用base64算法转换成等长字符串。
2、具体代码
package com.caokaiyuan.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class MD5Test
{
public static String MD5(String oldStr)
{
byte[] oldBytes = oldStr.getBytes();
MessageDigest md;
try
{
md = MessageDigest.getInstance("MD5");
byte[] newByte = md.digest(oldBytes);
BASE64Encoder encoder = new BASE64Encoder();
String newStr = encoder.encode(newByte);
return newStr;
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
return null;
}
}
}