Thorarin / crypto-js

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

Crypto-js decryption implemention #138

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, I coded 2 decryption implementations in ruby and javascript using crypto-js.
It appears the result doesn't match in both methods.

Here the implementation in ruby:

crypt_key = "6pqCt/xbHXinULTrPdMPhjGXc5IwEsIaGF9DWr0przk="
iv = "MWiW/yAGTLk2NTqK1MiZbw==\n"
data = "LbXJYXI3AFZtYfVIU47ZQ7O9S/ZgrjFGPLqCUkK13cekTFXbCil9pg/l/WCh\n91q6\n"
aes = OpenSSL::Cipher.new("AES-256-CBC")
aes.decrypt
aes.key = Base64.decode64(crypt_key)
aes.iv = Base64.decode64(iv)
final_key = aes.update(Base64.decode64(data)) + aes.final
puts "The server use this key for encryption: " + Base64.encode64(final_key)

output ======> "wPeLdB8FcAKoT9S9M3ukVRmg5AG7Aoom4GnojxogIxQ="

And in javascript with Crypto-js:

var crypt_key = 
CryptoJS.enc.Base64.parse("6pqCt/xbHXinULTrPdMPhjGXc5IwEsIaGF9DWr0przk=");
var iv = CryptoJS.enc.Base64.parse("MWiW/yAGTLk2NTqK1MiZbw==\n");
var data = 
CryptoJS.enc.Base64.parse("LbXJYXI3AFZtYfVIU47ZQ7O9S/ZgrjFGPLqCUkK13cekTFXbCil9p
g/l/WCh\n91q6\n");
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: data
});
var decrypted = CryptoJS.AES.decrypt(cipherParams, crypt_key, { iv: iv, mode: 
CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding });
console.debug(decrypted.toString(CryptoJS.enc.Base64));

output ======> 
"wPeLdB8FcAKoT9S9M3ukVRmg5AG7Aoom4GnojxogIxQFnttObsTPX3OiKD/sknvaRA=="

As you can see, only the first 43 characters matches and I don't know why.

Thank you in advance for helping me.

Original issue reported on code.google.com by spacew...@gmail.com on 31 Jul 2014 at 10:36

GoogleCodeExporter commented 9 years ago
It seems that using 

    var aesDecryptor = CryptoJS.algo.AES.createDecryptor(crypt_key, { iv: iv });
    var plaintextPart1 = aesDecryptor.process(data);

give the result we expected.
I guess it has to do with the aes.final.
If someone care to explain why the result differs it would be awesome, thanks.

Original comment by boris.ch...@youboox.fr on 22 Aug 2014 at 10:50