Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

javascript - CryptoJS解密格式错误的UTF-8数据错误(CryptoJS decryption Malformed UTF-8 data error)

I tried to encrypt and decrypt object.(我试图加密和解密对象。)

Encryption is working fine ,it turns encrypted string.(加密工作正常,它变成了加密的字符串。) But description function always returned me "Malformed UTF-8 data error"(但是描述函数总是返回我“格式错误的UTF-8数据错误”)

Object(宾语)

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'}]
        }]
    }

Encrypt(加密)

function encryptData2(data) {
var Key = C.enc.Utf8.parse("6il7YCRSqIOB9NooY225lPKQ0KuAF/nkFX6cY3vJkS0=");
        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);
    }
    var encryptedData = encryptData2(data);

Decrypt(解密)

 var decryptData2 = function(encryptedData, key) {
        var C = CryptoJS;
        encryptedData = C.enc.Base64.parse(encryptedData);
var Key = C.enc.Utf8.parse("6il7YCRSqIOB9NooY225lPKQ0KuAF/nkFX6cY3vJkS0=");
        var IV = C.enc.Utf8.parse("0123456789ABCDEF");
        var decryptedText = C.AES.encrypt(encryptedData, Key, {
            iv: IV,
            mode: C.mode.CBC,
            padding: C.pad.Pkcs7
        });
        return encryptedData.toString(CryptoJS.enc.Utf8)
    }

    var result = decryptData2(encryptedData);

How to resolve it ?(怎么解决呢?)

  ask by ryoshpa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 .(由于encryptdecrypt方法都处理各种数据类型(请参见The Cipher InputThe Cipher Output ),因此有几种方法可以解决encryptData2decryptData2之间的不兼容性。)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...