Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CryptoJS decrypt object

I need to decrypt something encrypted with CryptoJS. I have the function used to encrypt, the structure of the object encrypted and data used to encrypt to encrypt but I need to know some values of that object.

The function is:

var c = CryptoJS.enc.Utf8.parse(g.slice(0, 16));
var d = CryptoJS.AES.encrypt(JSON.stringify(collectData), c, {
    iv: c,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
}).toString()

Later, to the encrypted variable the following is applied:

d.toString().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '*');

I tried with this but I can't revert the object:

var decrypted = CryptoJS.AES.decrypt(coord, key, {
    iv: key,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});

console.log('decrypted clean:\n' + decrypted);
console.log('decrypted JSON.stringify():\n'+ JSON.stringify(decrypted));

thanks!

like image 836
sarasa Avatar asked Sep 18 '25 19:09

sarasa


1 Answers

Hey your question actually help me to find a solution, encrypt takes in a object, and decrypt takes in a string, so you can do your replace

The other place that could be giving this type of error is the key, when parsing the key to Utf8 ensure that the key is exactly 16 characters you can add nulls add the end of the string to ensure it is long enough by appending '/u0000' to your string before parsing

encrypt(data) {
return CryptoJS.AES.encrypt(JSON.stringify(data), this.secret,
 {
    keySize: 128 / 8,
    iv: this.secret,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  }).toString();
}

decrypt(data) {
return JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(data,  this.secret, 
{
    keySize: 128 / 8,
    iv: this.secret,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })));
}
like image 50
Jeffrey Skinner Avatar answered Sep 21 '25 08:09

Jeffrey Skinner