Since both, the encrypt
- and decrypt
-method process various data types (see The Cipher Input and The Cipher Output ), there are several ways to solve the incompatibilities between encryptData2
and decryptData2
.(由于encrypt
和decrypt
方法都处理各种数据类型(请参见The Cipher Input和The Cipher Output ),因此有几种方法可以解决encryptData2
和decryptData2
之间的不兼容性。)
One possibility is:(一种可能性是:)
const CryptoJS = require("crypto-js");
var data = {
'City': 1,
'DisplayOrder': 20,
'Semantic': [{'Value': '1','Description': [{'value': 'string'}],
'aff': [{'Id': '2','Name': 'Feature1','FeatureOptions': [{'Key': 'Key1','Value': 'Value1'},{ 'Key': 'Key2','Value': 'Value2'}],
'SelectedFeatureOption': { 'Key': 'Key1', 'Value': 'Value1'}}],'aff1': [{'Key': 'Key1','Value':'Value1'}]
}]
}
function encryptData2(data) {
//var Key = C.enc.Utf8.parse("6il7YCRSqIOB9NooY225lPKQ0KuAF/nkFX6cY3vJkS0=");
var Key = CryptoJS.enc.Utf8.parse("6il7YCRSqIOB9NooY225lPKQ0KuAF/nkFX6cY3vJkS0="); // 1. Replace C by CryptoJS
var IV = CryptoJS.enc.Utf8.parse("0123456789ABCDEF");
var encryptedText = CryptoJS.AES.encrypt(JSON.stringify(data), Key, {
iv: IV,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
//return encryptedText.toString(CryptoJS.format.Hex);
return encryptedText.toString(CryptoJS.format.Base64); // 2. Use Base64 instead of Hex
}
var encryptedData = encryptData2(data);
var decryptData2 = function(encryptedData, key) {
var C = CryptoJS;
//encryptedData = C.enc.Base64.parse(encryptedData); // 3. Remove line
var Key = C.enc.Utf8.parse("6il7YCRSqIOB9NooY225lPKQ0KuAF/nkFX6cY3vJkS0=");
var IV = C.enc.Utf8.parse("0123456789ABCDEF");
//var decryptedText = C.AES.encrypt(encryptedData, Key, {
var decryptedText = C.AES.decrypt(encryptedData, Key, { // 4. Use decrypt instead of encrypt
iv: IV,
mode: C.mode.CBC,
padding: C.pad.Pkcs7
});
//return encryptedData.toString(CryptoJS.enc.Utf8);
return decryptedText.toString(CryptoJS.enc.Utf8); // 5. Use decryptedText instead of encryptedData
}
var result = decryptData2(encryptedData);
console.log(result);
As a side note, the key seems to be Base64-encoded, so it is best parsed this way:(附带说明一下,该密钥似乎是Base64编码的,因此最好以这种方式进行解析:)
var Key = CryptoJS.enc.Base64.parse("6il7YCRSqIOB9NooY225lPKQ0KuAF/nkFX6cY3vJkS0=");
Update:(更新:)
Following @kelalaka's suggestion, decryptData2
should normally return a JSON object, since a JSON object is also passed to encryptData2
.(按照@kelalaka的建议, decryptData2
通常应返回一个JSON对象,因为JSON对象也已传递给encryptData2
。)
In this case, the JSON string in decryptData2
would have to be converted into a JSON object when it is returned, eg with:(在这种情况下, decryptData2
的JSON字符串在返回时必须转换为JSON对象,例如:)
return JSON.parse(decryptedText.toString(CryptoJS.enc.Utf8));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…