java中16位或32位MD5加密

匆匆过客 提交于 2020-01-07 16:04:23

原文链接:https://blog.csdn.net/u010197591/article/details/51483559   (侵删)

import java.security.MessageDigest;

public class MD5Helper {
    
    public static String encrypt32(String encryptStr) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(encryptStr.getBytes());
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16)
                    hexValue.append("0");
                hexValue.append(Integer.toHexString(val));
            }
            encryptStr = hexValue.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return encryptStr;
    }
 
    
    public static String encrypt16(String encryptStr) {
        return encrypt32(encryptStr).substring(8, 24);
    }
 
    public static void main(String[] args) {
        String encryptStr = "22222222222,./.,./.,./!@#$%^&*()";
        System.out.println(MD5Helper.encrypt32(encryptStr));
        System.out.println(MD5Helper.encrypt16(encryptStr));
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!