md5加密工具类

来源:互联网 发布:自动分辨率软件 编辑:程序博客网 时间:2024/05/20 14:43

   对数据进行md5加密,使用的工具类记录下来

public class EncryptUtil {    /**     * Encrypt byte array.     */    public static byte[] encrypt(byte[] source, String algorithm) throws NoSuchAlgorithmException {        MessageDigest md = MessageDigest.getInstance(algorithm);        md.reset();        md.update(source);        return md.digest();    }    /**     * Encrypt string     */    public static String encrypt(String source, String algorithm) throws NoSuchAlgorithmException {        byte[] resByteArray = encrypt(source.getBytes(), algorithm);        return StringUtils.toHexString(resByteArray);    }    /**     * Encrypt string using MD5 algorithm     */    public static String encryptMD5(String source) {        if (source == null) {            source = "";        }        String result = "";        try {            result = encrypt(source, "MD5");        }        catch (NoSuchAlgorithmException ex) {            ex.printStackTrace();        }        return result;    }    public static void main(String[] args) {        System.out.println(encryptMD5("sloan"));    }}

   

原创粉丝点击