MD5加密工具类

import java.security.MessageDigest;

public class CodeUtil
{

    public static String encode(String password, String algorithm)
    {
        byte[] unencodedPassword = password.getBytes();
        MessageDigest md = null;
        try
        {
            md = MessageDigest.getInstance(algorithm);
        }
        catch (Exception e)
        {
            return password;
        }
        md.reset();
        md.update(unencodedPassword);
        byte[] encodedPassword = md.digest();
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < encodedPassword.length; i++)
            {
            if ((encodedPassword[i] & 0xFF) < 16)
                {
                buf.append("0");
                }
            buf.append(Long.toString(encodedPassword[i] & 0xFF, 16));
            }
        return buf.toString();
        }

}

调用工具类传入需要加密的数据和加密算法名称:

String password=123456;
String encodePass=CodeUtil.encode(password, "MD5");

 

你可能感兴趣的:(Java)