CryptoJS encrypt in aes-256-cbc returns an unexpected value

43
August 08, 2019, at 10:10 PM

I am encrypting some data using CryptoJS and comparing it to an online tool and I am not getting the same result. In fact the result from CryptoJS in not decryptable with the tool.

I am trying to encrypt in AES-256-CBC with the following parameters:

text = '111222333'
iv = 'I8zyA4lVhMCaJ5Kg'
key = '6fa979f20126cb08aa645a8f495f6d85'

Here's my code:

let text = '111222333';
aesEncrypt(data) {
    let key = '6fa979f20126cb08aa645a8f495f6d85';    //length 32
    let iv = 'I8zyA4lVhMCaJ5Kg';                     //length 16
    let cipher = CryptoJS.AES.encrypt(data, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    return cipher.toString();
}
aesEncrypt(text);

The resulting encrypted string is U2FsdGVkX1+f3UywYmIdtb50bzdxASRCSqB00OijOb0= while the one obtained with the online tool is B6AeMHPHkEe7/KHsZ6TW/Q==. Why are they different, I seem to be using the same parameters?

My plan in using CryptoJS is to encrypt some data client side and then be able to decrypt it server side, if needed. But the differences in both encrypted values is stopping me to do so.

Answer 1

How about parsing your data as Utf8. Just like the "online tool" is doing. Use CryptoJS.enc.Utf8.parse

aesEncrypt(data) {
    let key = '6fa979f20126cb08aa645a8f495f6d85';
    let iv = 'I8zyA4lVhMCaJ5Kg';
    let cipher = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
        iv: CryptoJS.enc.Utf8.parse(iv),
        padding: CryptoJS.pad.Pkcs7,
        mode: CryptoJS.mode.CBC
    });
    return cipher.toString(); // B6AeMHPHkEe7/KHsZ6TW/Q==
}

Working example

READ ALSO
Problem shifting values in array of objects

Problem shifting values in array of objects

I have an array of objectsEach Object contains two values, x and y

22
Why doesn't async await work with axios correctly? [duplicate]

Why doesn't async await work with axios correctly? [duplicate]

This question already has an answer here:

49
Emit and On with Socket.io in two differents files

Emit and On with Socket.io in two differents files

I would like to know if it's possible to separate "emit" and "on" with socketio ?

40
feathersjs: ignore url query-parameter in database-query

feathersjs: ignore url query-parameter in database-query

is there a way to ignore URL-Query-parameters in database-querys?

56