brix / crypto-js

JavaScript library of crypto standards.
Other
15.8k stars 2.38k forks source link

Wrong Decryption of AES Encryption #183

Open marcogrieco opened 5 years ago

marcogrieco commented 5 years ago

Hi, in a javascript prject i'm using the standard encryption mode for AES.

CryptoJS.AES.encrypt(toEncrypt, key).toString();

Without any option.

Why, if i use the generated string and the provided key on another tool to decrypt i don't obtain the original string?

I'm using a key of 32 character.

Which is the default encryption mode? AES-CBC 256bit?

Thanks in advance.

Marco

gkpo commented 5 years ago

Same question here!

mxdpeep commented 5 years ago

post a complete example, please

gkpo commented 5 years ago

Say you encrypt some text with crypto-js:

AES.encrypt(text, secretkey).toString()

Encrypted text:

U2FsdGVkX189sEly0x7j2viW1qlJjFRCVhvzAagwnew9HIkNO8Ia7gRAbaAkdUdT

Now say you wanna decrypt this somewhere else. Here for instance:

http://aes.online-domain-tools.com/

As you can see, not only does it ask for a Key it also asks for a Mode and Function. So the question marcogrieco was asking is, what is the default mode and function.

Some libs even ask you for an initialisation vector (iv) parameter to decrypt the cypher. But with CryptoJs the iv is optional.

mxdpeep commented 5 years ago

CryptoJS supports AES-128, AES-192, and AES-256.

It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key. For the key, when you pass a string, it's treated as a passphrase and used to derive an actual key and IV. Or you can pass a WordArray that represents the actual key. If you pass the actual key, you must also pass the actual IV.

CryptoJS supports the following modes:

And CryptoJS supports the following padding schemes:

https://github.com/brix/crypto-js/blob/develop/docs/QuickStartGuide.wiki#the-cipher-input

s pozdravem,

Filip Oščádal fredbrooker.gscloud.cz https://fredbrooker.gscloud.cz

On Tue, Dec 25, 2018 at 12:11 PM sikko notifications@github.com wrote:

Say you encrypt some text with crypto-js:

AES.encrypt(text, secretkey).toString()

Encrypted text:

U2FsdGVkX189sEly0x7j2viW1qlJjFRCVhvzAagwnew9HIkNO8Ia7gRAbaAkdUdT

Now say you wanna decrypt this somewhere else. Here for instance:

http://aes.online-domain-tools.com/

As you can see, it asks for a mode and function. So the question marcogrieco was asking is, what is the default mode and function.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/brix/crypto-js/issues/183#issuecomment-449841060, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEmXvglpsxuSJ94Vu5fPCTO3-7Epveaks5u8gfrgaJpZM4X8Ze6 .

mxdpeep commented 5 years ago

in short: aes-256-cbc

s pozdravem,

Filip Oščádal fredbrooker.gscloud.cz https://fredbrooker.gscloud.cz

On Tue, Dec 25, 2018 at 12:11 PM sikko notifications@github.com wrote:

Say you encrypt some text with crypto-js:

AES.encrypt(text, secretkey).toString()

Encrypted text:

U2FsdGVkX189sEly0x7j2viW1qlJjFRCVhvzAagwnew9HIkNO8Ia7gRAbaAkdUdT

Now say you wanna decrypt this somewhere else. Here for instance:

http://aes.online-domain-tools.com/

As you can see, it asks for a mode and function. So the question marcogrieco was asking is, what is the default mode and function.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/brix/crypto-js/issues/183#issuecomment-449841060, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEmXvglpsxuSJ94Vu5fPCTO3-7Epveaks5u8gfrgaJpZM4X8Ze6 .

nirgal commented 5 years ago

cryptojs uses deprecated hash algorithm md5. openssl default is now sha256.

wingleungchoi commented 5 years ago

Hi @marcogrieco,

I think you might miss the argument CryptoJS.enc.Utf8. in toString function I got the same question and solved it by the following. CryptoJS.AES.encrypt(toEncrypt, key).toString(CryptoJS.enc.Utf8); ref: https://github.com/brix/crypto-js#plain-text-encryption

Hope it help.

nbastoWM commented 5 years ago

Hi, I also have this issue.

I encrypt with:

let toEncrypt = 'coco'; 
let key = "CASFDA123456FPOMD7890DAS12GR3456";

    let cipher = CryptoJS.AES.encrypt(
      toEncrypt,
      key, {
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
      }
    );

    let ciphertext = cipher.toString();

    console.log("encrypted cipher", cipher);
    console.log("encrypted cipher utf", CryptoJS.enc.Utf8.stringify(cipher));
    console.log("encrypted iv", cipher.iv.toString());
    console.log("encrypted key", cipher.key.toString());
    console.log("encrypted text", ciphertext);
    console.log("encrypted text b64", btoa(ciphertext));

With the results:

encrypted cipher {init: ƒ, $super: {…}, ciphertext: W…y.init, key: {…}, iv: {…}, …}
encrypted cipher utf 
encrypted iv ee13ba35509c2e4a81677c1f26ef026a
encrypted key 13a698e9326b86a958ef2113fbb24fa4cab3bad70a4cd57f0838806210856441
encrypted text U2FsdGVkX1+l/HcwzkdcgcV8N2hKHmoljiAzF8UoF0g=
encrypted text b64 VTJGc2RHVmtYMStsL0hjd3prZGNnY1Y4TjJoS0htb2xqaUF6RjhVb0YwZz0=

But I can't decrypt the text or text b64 using any online service like: https://encode-decode.com/aes-256-cbc-encrypt-online/ https://aesencryption.net/ https://www.devglan.com/online-tools/aes-encryption-decryption

As suggested by @wingleungchoi , I can't get any result using CryptoJS.enc.Utf8 either with toString(CryptoJS.enc.Utf8) or with stringify.

Any help appreciated.

nishant-batra commented 1 year ago

Hi, I also have this issue.

I encrypt with:

let toEncrypt = 'coco'; 
let key = "CASFDA123456FPOMD7890DAS12GR3456";

    let cipher = CryptoJS.AES.encrypt(
      toEncrypt,
      key, {
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
      }
    );

    let ciphertext = cipher.toString();

    console.log("encrypted cipher", cipher);
    console.log("encrypted cipher utf", CryptoJS.enc.Utf8.stringify(cipher));
    console.log("encrypted iv", cipher.iv.toString());
    console.log("encrypted key", cipher.key.toString());
    console.log("encrypted text", ciphertext);
    console.log("encrypted text b64", btoa(ciphertext));

With the results:

encrypted cipher {init: ƒ, $super: {…}, ciphertext: W…y.init, key: {…}, iv: {…}, …}
encrypted cipher utf 
encrypted iv ee13ba35509c2e4a81677c1f26ef026a
encrypted key 13a698e9326b86a958ef2113fbb24fa4cab3bad70a4cd57f0838806210856441
encrypted text U2FsdGVkX1+l/HcwzkdcgcV8N2hKHmoljiAzF8UoF0g=
encrypted text b64 VTJGc2RHVmtYMStsL0hjd3prZGNnY1Y4TjJoS0htb2xqaUF6RjhVb0YwZz0=

But I can't decrypt the text or text b64 using any online service like: https://encode-decode.com/aes-256-cbc-encrypt-online/ https://aesencryption.net/ https://www.devglan.com/online-tools/aes-encryption-decryption

As suggested by @wingleungchoi , I can't get any result using CryptoJS.enc.Utf8 either with toString(CryptoJS.enc.Utf8) or with stringify.

Any help appreciated.

The only working thing i could find was https://www.aesencryptiononline.com/2022/03/aes-encryption-function-ontools.html

hope this helps