Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Every time New Encrypted string Generate using AES #151

Open
AkibDeraiya123 opened this issue Feb 23, 2018 · 11 comments
Open

Every time New Encrypted string Generate using AES #151

AkibDeraiya123 opened this issue Feb 23, 2018 · 11 comments

Comments

@AkibDeraiya123
Copy link

Hello,
I'm using crypto-js library with Node.js Encryption/Decryption `(String/ Stringify object).

Issue is that when i try to Encrypt any string then every moment i get different different encrypted strings But when i Decrypt this all the strings then i got the same plain text Which i use for encrypt.

I want to do like same Encrypted string every moment.

Here my node and npm version

Node:- v8.9.1
NPM:- 5.5.1

My code is like this,

var CryptoJS = require("crypto-js");
const Key = '39067A6F8088F81E9C2BB5D46A8C0F60';
const text = JSON.stringify({Key1: 'value1', key2:'key2'});

// Encrypt 
var ciphertext = CryptoJS.AES.encrypt(text, Key);
console.log(ciphertext); // Here i got different different strings every moment for same plain text

// Decrypt 
var bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), Key);
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
 
console.log(JSON.parse(plaintext)); // Print like { Key1: 'value1', key2: 'key2' }

I want to do like that every moment i got same encrypted string for same plain text.

Please help me i'm in stuck.

Thank you in advance :)

@ayazzali
Copy link

maybe you can use sha or smth else?
couse aes has Salt, so every time we got different encripted strings .

@NuclearPhoenixx
Copy link

NuclearPhoenixx commented Apr 23, 2018

As @ayazzali already mentioned, you get different results each time because AES utilizes something called "salt" to make the encryption more secure. This salt is different every time you encrypt something.

Using SHA or any other hashing method will most likely not be the solution you're looking for since you cannot decrypt a hash. So I'm afraid you have to change the way you use AES cause you cannot change the way it works :/

@neckaros
Copy link

You can use AES-CBC and use a derivative of the Explicit Initialization Vector technique by specifying a fixed IV. But by having fixed result for a same input you loose in security.

@szhero
Copy link

szhero commented Sep 6, 2018

Use CryptoJS.enc.Utf8 to convert key .
It seems that your key is treated as a hex number.

@sanjayV
Copy link

sanjayV commented May 13, 2019

Thanks @szhero
I was struggling with this issue from last 5 days.
Now it working 👍

@sachsharma
Copy link

@sanjayV... I'm also using this technique in javascript and facing the same issue. I'm not getting the same encrypted key for the same set of input.

Please help me to resolve this.

Below is my code used to encrypt the string.

var key = '55a51621a6648525';
var keyutf = CryptoJS.enc.Hex.stringify(key);
var iv = CryptoJS.enc.Hex.stringify(key);
var encStr = CryptoJS.AES.encrypt("teststring", keyutf, { iv: iv });
console.log(encStr.ciphertext);

@sanjayV
Copy link

sanjayV commented May 17, 2019

Hi @sachsharma
I checked with your code and did some updates.
I am receiving same encrypt key each time and Encryption/Decryption working for me..

        const key = '55a51621a6648525';
        const keyutf = CryptoJS.enc.Utf8.parse(key);
        const iv = CryptoJS.enc.Base64.parse(key);
        const enc = CryptoJS.AES.encrypt("teststring", keyutf, { iv: iv });
        const encStr = enc.toString();

        console.log('encStr', encStr);

        const dec = CryptoJS.AES.decrypt(
            { ciphertext: CryptoJS.enc.Base64.parse(encStr) },
            keyutf,
            {
                iv: iv
            });
        const decStr = CryptoJS.enc.Utf8.stringify(dec)
        console.log('decStr', decStr);

@sachsharma
Copy link

Thanks @sanjayV 👍
Its working fine.

Any idea of getting the 64 bit or 128 bit or 256 bit length output based on the requirement. I have searched alot but not getting good idea.

@mariokaram
Copy link

mariokaram commented Oct 29, 2020

    ```const key = '55a51621a6648525';
    const keyutf = CryptoJS.enc.Utf8.parse(key);
    const iv = CryptoJS.enc.Base64.parse(key);
    const enc = CryptoJS.AES.encrypt("teststring", keyutf, { iv: iv });
    const encStr = enc.toString();

    console.log('encStr', encStr);

    const dec = CryptoJS.AES.decrypt(
        { ciphertext: CryptoJS.enc.Base64.parse(encStr) },
        keyutf,
        {
            iv: iv
        });
    const decStr = CryptoJS.enc.Utf8.stringify(dec)
    console.log('decStr', decStr);```

hey the deycrypt is not working

@WaqqarSuleman
Copy link

    const decStr = CryptoJS.enc.Utf8.stringify(dec)

This solution works for some inputs but not all.

@shardul28
Copy link

Hi @sachsharma I checked with your code and did some updates. I am receiving same encrypt key each time and Encryption/Decryption working for me..

        const key = '55a51621a6648525';
        const keyutf = CryptoJS.enc.Utf8.parse(key);
        const iv = CryptoJS.enc.Base64.parse(key);
        const enc = CryptoJS.AES.encrypt("teststring", keyutf, { iv: iv });
        const encStr = enc.toString();

        console.log('encStr', encStr);

        const dec = CryptoJS.AES.decrypt(
            { ciphertext: CryptoJS.enc.Base64.parse(encStr) },
            keyutf,
            {
                iv: iv
            });
        const decStr = CryptoJS.enc.Utf8.stringify(dec)
        console.log('decStr', decStr);

THANK YOU BHAI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants