核心提示:后端Java使用JDK的API进行des加密,需要前端解析javascript解密注意key和iv(向量)都应该是utf8格式的Java使用byte数组加密的,前端需要先转换为字符串,然后再转...
需要前端解析
javascript解密
注意key和iv(向量)都应该是utf8格式的
Java使用byte数组加密的,前端需要先转换为字符串,然后再转为utf8
<!DOCTYPE html> <html> <head> <title>des</title> <script type="text/javascript" src="../CryptoJS v3.1.2/rollups/tripledes.js"></script> </head> <body> </body> <script type="text/javascript"> function getStrFromBytes (arr) { var r = ""; for(var i=0;i<arr.length;i++){ r += String.fromCharCode(arr[i]); } console.log(r); return r; } var key = 'xxxxxxx'; // 秘钥 var message = 'wXgrbJBCU2vZqzTJRd6VP/evr5rQSC/VM7ckcFYXW8DeGOt/rRFIXpU0BDfTAMjMJHV9AC2xqNw='; // 待解密密文 var keyHex = CryptoJS.enc.Utf8.parse(key); // 将秘钥转换为utf8格式 var ivHex = CryptoJS.enc.Utf8.parse(getStrFromBytes([0x12, 0x34, 0x56,0x78, 0x90, 0xAB,0xCD, 0xEF ])); // 将向量装换位字符串再转为utf8 var decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Base64.parse(message) // 因为Java加密时进行了Base64编码,所以此处解码 }, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC, // 模式有很多种,由Java代码知道使用的是CBC padding: CryptoJS.pad.Pkcs7 // 填充模式有很多种,但是Java用的Pkcs5,此处Pkcs7也是可以解密的 }); console.log(decrypted); // 不转换为字符串是得不到字符串结果的 console.log(decrypted.toString(CryptoJS.enc.Utf8)); </script> </html>
Java加密
public class DESUtil { private static final byte[] DES_IV = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF };// 设置向量,略去 public static String encode(String data, String key) throws Exception { DESKeySpec keySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher enCipher.init(Cipher.ENCRYPT_MODE, keyFactory.generateSecret(keySpec), new IvParameterSpec(DES_IV));// 设置工作模式为加密模式,给出密钥和向量 byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8")); BASE64Encoder base64Encoder = new BASE64Encoder(); return base64Encoder.encode(pasByte); } public static String decode(String data, String key) throws Exception { DESKeySpec keySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); deCipher.init(Cipher.DECRYPT_MODE, keyFactory.generateSecret(keySpec), new IvParameterSpec(DES_IV)); BASE64Decoder base64Decoder = new BASE64Decoder(); byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data)); return new String(pasByte, "UTF-8"); } public static void main(String[] args) throws Exception { System.out.println("加密:" + DESUtil.encode("innersect://navigation?target=product&id=2935&source=wxapp", "tmmt2017")); System.out.println("加密:" + DESUtil.encode("innersect://navigation?target=reserve&id=15", "tmmt2017")); System.out.println("加密:" + DESUtil.encode("innersect://navigation?target=reserve&id=17", "tmmt2017")); System.out.println("解密:" + DESUtil.decode("wXgrbJBCU2vZqzTJRd6VP/evr5rQSC/VM7ckcFYXW8DeGOt/rRFIXpc6NZH5S/4D", "tmmt2017")); System.out.println("解密:" + DESUtil.decode("wXgrbJBCU2vZqzTJRd6VP/evr5rQSC/VM7ckcFYXW8DeGOt/rRFIXgg8CI/CQ78h", "tmmt2017")); } }