java字符串MD5加密后再转16进制

话不多说上码

pom.xml

 <!-- MD5 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>

 

 

public static byte[] digest(String signStr) {
        MessageDigest md5Instance = null;
        try {
            md5Instance = MessageDigest.getInstance("MD5");
            md5Instance.update(signStr.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return md5Instance.digest();
    }

得到byte数据,转16进制后的字符串

//加密后转
        private static String byte2Hex(byte[] bytes) {
            char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
            int j = bytes.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (byte byte0 : bytes) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        }

最终得到你想要的

posted on 2019-05-24 11:43  东子z  阅读(1152)  评论(0编辑  收藏  举报

导航