package com.boying.common.util; import org.apache.tomcat.util.security.MD5Encoder; import org.springframework.util.DigestUtils; import sun.security.provider.MD5; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * MD5加密工具类 */ public class MD5Util { public static String getMD5String(String str) { try { // 生成一个MD5加密计算摘要 MessageDigest md = MessageDigest.getInstance("MD5"); // 计算md5函数 md.update(str.getBytes()); // digest()最后确定返回md5 hash值,返回值为8位字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符 // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值 //一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方) return new BigInteger(1, md.digest()).toString(16); } catch (Exception e) { e.printStackTrace(); return null; } } private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; public static String byteArrayToHexString(byte[] b) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) { resultSb.append(byteToHexString(b[i])); } return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } /** * 加密 * * @param origin * 原始字符串 * @return 32位字符串 */ public static String encode(String origin) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); resultString = byteArrayToHexString(md.digest(resultString .getBytes())); } catch (Exception ex) { System.out.println(ex); } return resultString; } /** * * @param origin * @param charsetname * @return */ public static String encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString .getBytes())); else resultString = byteArrayToHexString(md.digest(resultString .getBytes(charsetname))); } catch (Exception exception) { } return resultString; } /** * 发短信加密使用 * * @param source * @return */ public static String encrypt(String source) { if (source == null) { source = ""; } String result = ""; try { result = encrypt(source, "MD5"); } catch (NoSuchAlgorithmException ex) { // this should never happen throw new RuntimeException(ex); } return result; } private static String encrypt(String source, String algorithm) throws NoSuchAlgorithmException { byte[] resByteArray = encrypt(source.getBytes(), algorithm); return toHexString(resByteArray); } private static byte[] encrypt(byte[] source, String algorithm) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(algorithm); md.reset(); md.update(source); return md.digest(); } private static String toHexString(byte[] res) { StringBuffer sb = new StringBuffer(res.length << 1); for (int i = 0; i < res.length; i++) { String digit = Integer.toHexString(0xFF & res[i]); if (digit.length() == 1) { digit = '0' + digit; } sb.append(digit); } return sb.toString().toLowerCase(); } public static void main(String[] args) { String s ="amt=1.00¬ifyUrl=http://test/payNotify&payKey=3ed6f127559e4f2f80e5e1cb0231b76b&payerName=tom&payerNum=54c51b276a9a47329b295662122968e1&payerTypeCode=130000451000011&paySecret=492f497a17cc41d19a757ae9d5809238"; System.out.println(encode(s)); String s2="amt=1.00¬ifyUrl=http://test/payNotify&payKey=3ed6f127559e4f2f80e5e1cb0231b76b&paySecret=492f497a17cc41d19a757ae9d5809238&payerName=峰峰&payerNum=3aa0f6499acb45f686b6442229d23b31&payerTypeCode=130000451000012"; System.out.println(DigestUtils.md5DigestAsHex(s.toString().getBytes())); } }