您的位置:网站首页 > 动态 > 动态

md5在线加密(16位md5在线解密)

2022-09-25 19:37     发布者:高夕
导读加密package cn.zx;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;impor

加密

package cn.zx;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MD5Util {

    
    public static String md5Encrypt32Upper(String string) {
        byte[] hash;
        try {
            //创建一个MD5算法对象,并获得MD5字节数组,16*8=128位
            hash = MessageDigest.getInstance(&34;MD5&34;).digest(string.getBytes(&34;UTF-8&34;));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(&34;Huh, MD5 should be supported?&34;, e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(&34;Huh, UTF-8 should be supported?&34;, e);
        }

        //转换为十六进制字符串
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append(&34;0&34;);
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString().toUpperCase();
    }

    
    public static String md5Encrypt32Lower(String string) {
        //直接上面的方法转换成小写就可以了
        return md5Encrypt32Upper(string).toLowerCase();
    }

    
    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(&34;0&34;);
            }
            hexStr.append(Integer.toHexString(num));
        }
        return hexStr.toString().toUpperCase();
    }

    
    public static String unicodeToString(String str) {
        Pattern pattern = Pattern.compile(&34;(\\u(\p{XDigit}{4}))&34;);
        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 + &34;&34;);
        }
        return str;
    }

    public static void main(String[] args) {
        System.out.println(md5Encrypt32Lower(&34;123456&34;));
        System.out.println(md5Encrypt32Upper(&34;123456&34;));
    }
}
免责声明:本文章由会员“高夕”发布如果文章侵权,请联系我们处理,本站仅提供信息存储空间服务如因作品内容、版权和其他问题请于本站联系

猜你喜欢

最新文章