hibin101 / crypto-js

Automatically exported from code.google.com/p/crypto-js
0 stars 0 forks source link

Not able to decrypt the encrypted data on server side which was encrypted using CryproJS on client side.. #140

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I want to encrypt some data on client side and Decrypt it on the server side 
but it is giving error on the server side saying that "Incorrect key".

Followings are my code snippets

        var key = "randomKey";

        var hash  = CryptoJS.MD5(key);

        var hashedKey = btoa(hash.toString(CryptoJS.enc.Base64));

        var iv  = "1234567812345678";
        var message   = "#This is the plain text to encrypt#";

        var encrypted = CryptoJS.AES.encrypt(message, hashedKey, {iv : iv, mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7} );

        var encrypteData=encrypted.toString(CryptoJS.enc.utf8);

That's how I am encrypting at client side. and sending "iv", "hashedKey" and 
"encryptedData" to server side.

we are decrypting this encrypted data at server side using java. we are using 
AES/CBC/PKCS5Pading. By passing iv, key and encrypted message to the "doFinal" 
method but we are getting error message as "Given final block not padded 
properly"

Cipher decrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
                decrypt.init(Cipher.DECRYPT_MODE, hashedKey,iv);

        byte[] decryptedMsg = decrypt.doFinal(encryptedMsg);

What am i doing wrong?

Original issue reported on code.google.com by tiwari.a...@gmail.com on 14 Aug 2014 at 9:43