以下是使用CryptoJS AES解密非字符串数组的示例代码:
假设我们有一个加密的二进制数组,密钥为“1234567890123456”,IV为“abcdef1234567890”,我们可以使用以下代码进行解密:
const encryptedData = [99, 157, 164, 206, 225, 160, 28, 83, 96, 84, 237, 219, 123, 147, 243, 50];
const key = CryptoJS.enc.Utf8.parse("1234567890123456");
const iv = CryptoJS.enc.Utf8.parse("abcdef1234567890");
// Decrypt the data
const decrypted = CryptoJS.AES.decrypt({
ciphertext: CryptoJS.lib.WordArray.create(encryptedData)
}, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
// Convert the decrypted data to a byte array
const decryptedBytes = new Uint8Array(decrypted.words.length * 4);
for (let i = 0; i < decrypted.words.length; i++) {
decryptedBytes[i * 4 + 3] = (decrypted.words[i] >> 24) & 0xff;
decryptedBytes[i * 4 + 2] = (decrypted.words[i] >> 16) & 0xff;
decryptedBytes[i * 4 + 1] = (decrypted.words[i] >> 8) & 0xff;
decryptedBytes[i * 4] = decrypted.words[i] & 0xff;
}
console.log(decryptedBytes); // Output: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] (which is "Hello World" in ASCII)
在上面的代码中,我们首先将加密的数据转换为CryptoJS使用的WordArray格式。然后,我们使用密钥和IV调用CryptoJS.AES.decrypt()方法进行解密。最后,我们将解密的数据转换回原始的二进制数组
原文地址: https://www.cveoy.top/t/topic/c4mC 著作权归作者所有。请勿转载和采集!