package com.t3.ts.driver.resume.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import java.security.Key; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.spec.AlgorithmParameterSpec; /** * @class_name: MD5Tool * @description: * @author: wm_yu * @create: 2019/09/12 **/ public class MD5Tool { private final static Logger log = LoggerFactory.getLogger(MD5Tool.class); /** * 向量(同时拥有向量和密匙才能解密),此向量必须是8byte,多少都报错 */ private final static byte[] DESIV = new byte[] { 0x22, 0x54, 0x36, 110, 0x40, (byte) 0xac, (byte) 0xad, (byte) 0xdf }; /** * 加密算法的参数接口 */ private static AlgorithmParameterSpec iv = null; private static Key key = null; private static String charset = "utf-8"; private static String SECRET_KEY = "d5416a341766390368ab75d220a6c051"; static { try { // 设置密钥参数 DESKeySpec keySpec = new DESKeySpec(SECRET_KEY.getBytes(charset)); // 设置向量 iv = new IvParameterSpec(DESIV); // 得到密钥工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 获得密钥对象 key = keyFactory.generateSecret(keySpec); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { String source = "123456789"; String value = md5Encode(source); String decode = md5Decode(value); System.out.println("原始数据:" + source + "----加密后的数据:" + value + "-----解密后的数据:" + decode); } catch (Exception e) { e.printStackTrace(); } } /** * 加密 * @param data * @return * @throws Exception */ public static String md5Encode(String data) throws Exception { // 获得加密对象Cipher Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // 设置工做模式为加密模式,给出密钥和向量 enCipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] pasByte = enCipher.doFinal(data.getBytes(charset)); BASE64Encoder base64Encoder = new BASE64Encoder(); return base64Encoder.encode(pasByte); } /** * 解密 * @param data * @return * @throws Exception */ public static String md5Decode(String data) throws Exception { Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); deCipher.init(Cipher.DECRYPT_MODE, key, iv); BASE64Decoder base64Decoder = new BASE64Decoder(); //此处注意doFinal()的参数的位数必须是8的倍数,不然会报错(经过encode加密的字符串读出来都是8的倍数位,但写入文件再读出来,就可能由于读取的方式的问题,致使最后此处的doFinal()的参数的位数不是8的倍数) //此处必须用base64Decoder,若用data。getBytes()则获取的字符串的byte数组的个数很可能不是8的倍数,并且不与上面的BASE64Encoder对应(即便解密不报错也不会获得正确结果) byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data)); return new String(pasByte, charset); } /** * 获取MD5的值,可用于对比校验 * @param sourceStr * @return */ private static String getMD5Value(String sourceStr) { String result = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(sourceStr.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0){ i += 256;} if (i < 16){buf.append("0");} buf.append(Integer.toHexString(i)); } result = buf.toString(); } catch (NoSuchAlgorithmException e) { } return result; } }