工具篇——MD5Util(加密字符串)

代码如下:

package com.suirui.meetingcontrol.service;

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* 1.MD5加密字符串(32位大写)

* 2.MD5加密字符串(32位小写)

* <p>

* MD5在线加密:https://md5jiami.51240.com/

* 3.将二进制字节数组转换为十六进制字符串

* 4.Unicode中文编码转换成字符串

*/

public class MD5Util {

    /**

     * MD5加密字符串(32位大写)

     *

     * @param string 需要进行MD5加密的字符串

     * @return 加密后的字符串(大写)

     */

    public static String md5Encrypt32Upper(String string) {

        byte[] hash;

        try {

            //创建一个MD5算法对象,并获得MD5字节数组,16*8=128位

            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));

        } catch (NoSuchAlgorithmException e) {

            throw new RuntimeException("Huh, MD5 should be supported?", e);

        } catch (UnsupportedEncodingException e) {

            throw new RuntimeException("Huh, UTF-8 should be supported?", e);

        }

        //转换为十六进制字符串

        StringBuilder hex = new StringBuilder(hash.length * 2);

        for (byte b : hash) {

            if ((b & 0xFF) < 0x10) hex.append("0");

            hex.append(Integer.toHexString(b & 0xFF));

        }

        return hex.toString().toUpperCase();

    }

    /**

     * MD5加密字符串(32位小写)

     *

     * @param string 需要进行MD5加密的字符串

     * @return 加密后的字符串(小写)

     */

    public static String md5Encrypt32Lower(String string) {

        byte[] hash;

        try {

            //创建一个MD5算法对象,并获得MD5字节数组,16*8=128位

            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));

        } catch (NoSuchAlgorithmException e) {

            throw new RuntimeException("Huh, MD5 should be supported?", e);

        } catch (UnsupportedEncodingException e) {

            throw new RuntimeException("Huh, UTF-8 should be supported?", e);

        }

        //转换为十六进制字符串

        StringBuilder hex = new StringBuilder(hash.length * 2);

        for (byte b : hash) {

            if ((b & 0xFF) < 0x10) hex.append("0");

            hex.append(Integer.toHexString(b & 0xFF));

        }

        return hex.toString().toLowerCase();

    }

    /**

     * 将二进制字节数组转换为十六进制字符串

     *

     * @param bytes 二进制字节数组

     * @return 十六进制字符串

     */

    public static String bytesToHex(byte[] bytes) {

        StringBuffer hexStr = new StringBuffer();

        int num;

        for (int i = 0; i < bytes.length; i++) {

            num = bytes[i];

            if (num < 0) {

                num += 256;

            }

            if (num < 16) {

                hexStr.append("0");

            }

            hexStr.append(Integer.toHexString(num));

        }

        return hexStr.toString().toUpperCase();

    }

    /**

     * Unicode中文编码转换成字符串

     */

    public static String unicodeToString(String str) {

        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");

        Matcher matcher = pattern.matcher(str);

        char ch;

        while (matcher.find()) {

            ch = (char) Integer.parseInt(matcher.group(2), 16);

            str = str.replace(matcher.group(1), ch + "");

        }

        return str;

    }

}

在项目中的应用:

String md5Encrypt32Lower = MD5Util.md5Encrypt32Lower("0123456789");

String md5Encrypt32Upper = MD5Util.md5Encrypt32Upper("0123456789");

LogUtil.e("MD5", "大写=" + md5Encrypt32Upper + ",小写=" + md5Encrypt32Lower);

早计划,早准备,早完成。 欢迎关注!交流!Star!

GitHub:https://github.com/wangyang0313

微信公众号:一个灵活的胖子MrWang

CSDN:https://blog.csdn.net/qq941263013

20200430145304899.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 一世心甘做马牛,千夫共指怒横头。 乾坤落笔清风起,唤醒神州灭日仇。 作者王永豪(注:新韵)
    王永豪阅读 365评论 0 5
  • A向上阅读 179评论 0 4
  • 习惯就是别人做起来很别扭的事情而你却做的很自然。行动变成习惯,习惯变成性格,性格决定命运。
    康淳阅读 97评论 0 0
  • 不想一个人睡去,也不想一个人醒来,因为我们注定要一个人离开这世界。
    诗心的刺猬阅读 333评论 0 0