我的代码应从服务器获取加密的纯文本,并使用javascript将其解密。我在Java中具有解密功能,但是如何将其转换为javascript?
我尝试用cryptojs失败。因为密码IV只是字节数组和javascript没有字节数据类型
public static String decrypt(String plainText, String key) throws Exception {
byte[] clean = new BASE64Decoder().decodeBuffer(plainText);
int keySize = 16;
byte[] keyBytes = new byte[keySize];
byte[] pwbyte = key.getBytes("UTF-8");
int len = pwbyte.length;
if (len > keyBytes.length) {
len = keyBytes.length;
}
System.arraycopy(pwbyte, 0, keyBytes, 0, len);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(clean);
return new String(encrypted, "UTF-8");
}