一、代码
public class MD5 { public static void main(String[] args) { String md5 = getMD5("210534"); System.out.println(md5); System.out.println(md5.length()); String encrypt = encrypt("210534"); System.out.println(encrypt); System.out.println(encrypt.length()); } // private static final String slat = "&Q23***&&%%$$#@"; /** * 生成md5 * @param * @return */ public static String getMD5(String str) { // String base = str +"/"+slat; String base = str ; String md5 = DigestUtils.md5DigestAsHex(base.getBytes()); return md5; } public static String encrypt(String dataStr) { try { // dataStr = dataStr ; MessageDigest m = MessageDigest.getInstance("MD5"); m.update(dataStr.getBytes("UTF8")); byte s[] = m.digest(); String result = ""; for (int i = 0; i < s.length; i++) { result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6); } return result; } catch (Exception e) { e.printStackTrace(); } return ""; } }结果:
0aa9261c42527f4daa0c852941f51525 32 0aa9261c42527f4daa0c852941f51525 32备注:
具体是否需要加 String slat = "&Q23***&&%%$$#@";这一段,看需求。
原文:
https://blog.csdn.net/junmoxi/article/details/80841555