原文链接: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)); } }
来源:https://www.cnblogs.com/cmz-32000/p/12161675.html