node.js - 为什么 node.js 使用 Crypto.js 加密,但解密失败?
问题描述
我使用 json 来加密这些值。但是,我尝试再次解密该值,但没有成功。该值为空。
node.js 源码
import CryptoJS from 'crypto-js';
const EncryptHex = (string, chip) => {
let result = '';
try {
const key = CryptoJS.enc.Hex.parse(STORE_KEY);
if (chip === 'AES') {
const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
result = CryptoJS.AES.encrypt(string, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
});
result = result.ciphertext.toString(CryptoJS.enc.Hex);
DecryptHex(result, 'AES', STORE_KEY);
} else {
result = CryptoJS.HmacSHA256(string, key).toString(CryptoJS.enc.Hex);
}
return result;
} catch (error) {
throw error;
}
};
const DecryptHex = (string, chip, skey) => {
let result = '';
const key = CryptoJS.enc.Hex.parse(skey);
const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
try {
const array = CryptoJS.enc.Hex.parse(string);
if (chip === 'AES') {
result = CryptoJS.AES.decrypt(array, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
}).toString();
console.log('$$$$');
console.log('result : ', result);
console.log('$$$$');
} else {
result = array;
}
return result;
} catch (error) {
throw error;
}
};
对数值
$$$$
result :
$$$$
是什么原因?我怎么解决这个问题?
解决方案
我们需要做几件事,第一,获得正确的密钥长度,这应该是 128,192 或 256 位长。
在这段代码中,我使用的是 256 位密钥。不要在生产中使用它:)
此外,如果您在解密时使用格式:CryptoJS.format.Hex,它将接受十六进制编码的密文。
这应该加密然后解密测试消息:
// Using a 256-bit key to ensure we use AES-256
STORE_KEY = "7246b9b8171743cd9cf285367a2fd5ad";
const EncryptHex = (string, chip, skey) => {
let result = '';
try {
const key = CryptoJS.enc.Hex.parse(skey);
if (chip === 'AES') {
const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
result = CryptoJS.AES.encrypt(string, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
});
result = result.ciphertext.toString(CryptoJS.enc.Hex);
} else {
result = CryptoJS.HmacSHA256(string, key).toString(CryptoJS.enc.Hex);
}
return result;
} catch (error) {
throw error;
}
};
const DecryptHex = (encryptedStringHex, chip, skey) => {
let result = '';
const key = CryptoJS.enc.Hex.parse(skey);
const iv = CryptoJS.lib.WordArray.create([0x00, 0x00, 0x00, 0x00]);
try {
const array = CryptoJS.enc.Hex.parse(encryptedStringHex);
if (chip === 'AES') {
result = CryptoJS.AES.decrypt(encryptedStringHex, key, {
iv: iv,
format: CryptoJS.format.Hex,
mode: CryptoJS.mode.CBC,
}).toString(CryptoJS.enc.Utf8);
console.log('$$$$');
console.log('result : ', result);
console.log('$$$$');
} else {
result = array;
}
return result;
} catch (error) {
throw error;
}
};
let encrypted = EncryptHex("A good story cannot be devised; it has to be distilled", "AES", STORE_KEY);
console.log("Encrypted data:", encrypted);
console.log("Decrypted data:");
DecryptHex(encrypted, "AES", STORE_KEY);
推荐阅读
- kotlin - 如何使用 getHour() 在 kotlin 中获取当前时间
- typescript - 如何使用 NestJS 将选项传递给我的 AppModule?
- c - Why n++==--n always equal to 1?
- node.js - 从文件系统切换到 mongodb 方法后,如何重新创建嵌套目录迭代?
- flutter - 颤振标记:标记无法识别
- r - 在 R 中创建一个包含日期的列表
- mysql - SQL数据库数据排序
- python - python中关于Thread Pool Executor和Thread的一些疑惑
- kubernetes - Kubernetes:用 configmap 替换文件
- javascript - Shopify 创建被验证码挑战阻止的客户