brix / crypto-js

JavaScript library of crypto standards.
Other
15.83k stars 2.39k forks source link

Bug: AES.Decrypt(CryptoJS.enc.Base64.parse(cipherText) .....) returns empty Array #192

Open alex254 opened 5 years ago

alex254 commented 5 years ago

Hi guys,

I have been struggling with a bug, but I found the problem it seems:

var decoded = CryptoJS.enc.Base64.parse(cipherText); //base64 string to word array var decodedKey = CryptoJS.enc.Base64.parse(key); //base64 string to word array var decodedVector = CryptoJS.enc.Base64.parse(initVector); //base64 string to word array

var options = { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: decodedVector, keySize: 128 / 8 };

var decryptedBytes = CryptoJS.AES.decrypt(decoded, decodedKey, options);

decryptedBytes returns empty results. I bypassed the problem by adding:

decoded.ciphertext = decoded;

When inputting a SerializableCipher it expects a property 'ciphertext' that actually holds the cipher. When one does a roundtrip -> encrypt -> decrypt it works immediately because the result of the encrypt function results in a more complex object with a 'ciphertext' property. But coming straight from a base64 string one runs into problems because it is just a wordArray.

dvdknaap commented 4 years ago

I know this is an old issue but i got the same issue did you fixed this issue ?

runkelstein commented 3 years ago

This should be fixed immediately, I just wasted hours on this. I thought I had some weird utf8, utf16 encoding issue between my java and java script version. I debugged for ours. But couldn't go as deep as debugging into the cryptojs library itself (since I worked in postman with it).

alex your workaround still works in 2021