PrabathSigh
PrabathSigh

Reputation: 33

JavaScript AES Encryption showing wrong result against Java Android

I try to convert a string using AES method in JavaScript also I only need to use the CryptoJs library to achieve this.

When I try online from this website the result is coming as I expected which my Java program can decrypt it but when I try JavaScript I don't get the same result I get from the website.

This is what I need;

Data: {"test":1}
Secret: NdRgUkXp2s5v8y/A

The result should be: chib8X9Fnr7Vtn4VLRybKg==

The reason I'm referring the website, I don't know the mode and padding method of the above result but the below result is showing as expected from the server if you know the mode of the above result you can refer that also.

You can refer online encryption using https://www.devglan.com/online-tools/aes-encryption-decryption to validate.

This is what I get when I use JavaScript which is wrong;

var encrypted = CryptoJS.AES.encrypt('{"test",1}', "NdRgUkXp2s5v8y/A");
console.log(encrypted.toString()) 

Result: U2FsdGVkX1/GS5CecHJ10Z4qvSP8hY1NkDNtKGlg3OE= Also it changes each time when generate.

I use this library https://cryptojs.gitbook.io/docs/

Upvotes: 2

Views: 603

Answers (2)

Damilare Koiki
Damilare Koiki

Reputation: 462

When you are printing out the decrypted string don't forget to add .toString(CryptoJs.enc.utf8).

Check https://github.com/brix/crypto-js#plain-text-encryption

Upvotes: 0

Googlian
Googlian

Reputation: 6703

You pass the plain string value as a secret instead of UTF-8 encoded text and the value you get from the mentioned website is encrypted using PKCS7 padding method with ECB mode. Usually, this is the method most of the Android Java encryption methods support.

Encryption using CryptoJS.pad.Pkcs7 and make sure you provide the secret as UTF-8 as CryptoJS.enc.Utf8.parse(key) not in plain text.

let data = '{"test":1}'
let key = 'NdRgUkXp2s5v8y/A'

var encrypted = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});

console.log(encrypted.toString());

Decryption method

var decrypted = CryptoJS.AES.decrypt(encrypted.toString(), CryptoJS.enc.Utf8.parse(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
})

console.log(decrypted.toString(CryptoJS.enc.Utf8))

The below example proves the encrypted value matches as expected.

let data = '{"test":1}'
let key = 'NdRgUkXp2s5v8y/A'

/*
* Encryption
*/
var encrypted = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});

console.log(encrypted.toString());

/*
* Decryption
*/
var decrypted = CryptoJS.AES.decrypt(encrypted.toString(), CryptoJS.enc.Utf8.parse(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
})

console.log(decrypted.toString(CryptoJS.enc.Utf8) + "\n")
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

Upvotes: 2

Related Questions