3行代码解决MD5加密

2019-01-04  本文已影响0人  皎洁de时光

public class Md5Utils {

            /**

            * 利用MD5进行加密

            * @param str 待加密的字符串

            * @return 加密后的字符串

            * @throws NoSuchAlgorithmException

            * @throws UnsupportedEncodingException

            */

            public static String EncoderByMd5(String str) {

                    if (str == null) {

                            return null;

                    }

                    try {

                            // 确定计算方法

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

                            BASE64Encoder base64en = new BASE64Encoder();

                            // 加密后的字符串

                            return base64en.encode(md5.digest(str.getBytes("utf-8")));

                    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {

                            return null;

                    }

        }

}

上一篇 下一篇

猜你喜欢

热点阅读