cryptoJs DES_CBC_Pkcs7 转成 Java
前端 DES 加密:
import cryptoJs from 'crypto-js';
// DES 加密
function encrypt(message, key, iv) {
// 字符串转 16进制
const keyHex = cryptoJs.enc.Utf8.parse(key);
const ivHex = cryptoJs.enc.Utf8.parse(iv);
const option = {
iv: ivHex,
mode: cryptoJs.mode.CBC,
padding: cryptoJs.pad.Pkcs7,
};
// 加密
const encrypted = cryptoJs.DES.encrypt(message, keyHex, option);
console.log(
'------------encrypted------------',
'\nmessage: ' + message,
'\nkey: ' + key,
'\niv: ' + iv,
'\nkeyHex: ' + keyHex,
'\nivHex: ' + ivHex,
'\nencrypted: ' + encrypted, // 将 encrypted 转成 BASE64 字符串
'\nencrypted.ciphertext: ' + encrypted.ciphertext.toString() // 将 encrypted 转成十六进制字符串
);
return encrypted.ciphertext.toString();
}
// 调用加密方法
let encrypted = encrypt('{"name":"xiaoming","id":"112233","sex":"0"}', '12345678', '12345678');
打印结果:
------------encrypted------------
message: {"name":"xiaoming","id":"112233","sex":"0"}
key: 12345678
iv: 12345678
keyHex: 3132333435363738
ivHex: 3132333435363738
encrypted: nkzh3knwg2TwyS2Tmslfbk6a10DRqO/Sc2IMJ6abKXAsdkrR1z8X7LFJnCRbtV+s
encrypted.ciphertext: 9e4ce1de49f08364f0c92d939ac95f6e4e9ad740d1a8efd273620c27a69b29702c764ad1d73f17ecb1499c245bb55fac
对应的 Java 代码为:
/**
* 算法类型
*/
private final static String ALGORITHM_DES = "DES";
/**
* 加密算法/加密模式/填充类型
*/
private static final String CIPHER_DES_MODE_CBC_PKCS5PADDING = "DES/CBC/PKCS5Padding";
/**
* 字符编码
*/
public static final String CHARSET_NAME = "UTF-8";
/**
* 字节数组转成16进制字符串
*
* @param bytes
* @return
*/
public static String byte2hex(byte[] bytes) {
StringBuffer sb = new StringBuffer(bytes.length * 2);
String tmp = "";
for (int n = 0; n < bytes.length; n++) {
// 整数转成十六进制表示
tmp = (Integer.toHexString(bytes[n] & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase();
}
/**
* DES 加密
*
* @param data
* @param key
* @param iv
* @return
* @throws Exception
*/
public static String encryptCbcPkcs5padding(String data, String key, String iv) throws Exception {
Cipher cipher = Cipher.getInstance(CIPHER_DES_MODE_CBC_PKCS5PADDING);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), ALGORITHM_DES);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] result = cipher.doFinal(data.getBytes(CHARSET_NAME));
// String encrypted = new sun.misc.BASE64Encoder().encode(result);
// System.out.println("encrypted: " + encrypted);
String ciphertext = byte2hex(result);
return ciphertext.toLowerCase();
}
前端 DES 解密:
function decrypt(ciphertext, key, iv) {
const keyHex = cryptoJs.enc.Utf8.parse(key);
const ivHex = cryptoJs.enc.Utf8.parse(iv);
// 解密
const decrypted = cryptoJs.DES.decrypt(
{
ciphertext: cryptoJs.enc.Hex.parse(ciphertext),
},
keyHex,
{
iv: ivHex,
mode: cryptoJs.mode.CBC,
padding: cryptoJs.pad.Pkcs7,
},
);
console.log(
'------------decrypted------------',
'\nciphertext: ' + ciphertext,
'\nkey: ' + key,
'\niv: ' + iv,
'\nkeyHex: ' + keyHex,
'\nivHex: ' + ivHex,
'\ndecrypted.toString(cryptoJs.enc.Utf8): ' + decrypted.toString(cryptoJs.enc.Utf8)
);
return decrypted.toString(cryptoJs.enc.Utf8);
}
打印结果:
------------decrypted------------
ciphertext: 9e4ce1de49f08364f0c92d939ac95f6e4e9ad740d1a8efd273620c27a69b29702c764ad1d73f17ecb1499c245bb55fac
key: 12345678
iv: 12345678
keyHex: 3132333435363738
ivHex: 3132333435363738
decrypted.toString(cryptoJs.enc.Utf8): {"name":"xiaoming","id":"112233","sex":"0"}
对应的 Java 代码为:
/**
* DES 解密
*
* @param dataEncode
* @param key
* @param iv
* @return
* @throws Exception
*/
public static String decryptCbcPkcs5padding(String dataEncode, String key, String iv) throws Exception {
byte[] encrypted = Convert.hexToBytes(dataEncode);
Cipher cipher = Cipher.getInstance(CIPHER_DES_MODE_CBC_PKCS5PADDING);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), ALGORITHM_DES);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
return new String(cipher.doFinal(encrypted));
}