Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES Encryption in Crypto Js and Rails are giving different results

I am trying to encrypt text using AES-256-CBC with Crypto Js (on the client side) and OpenSSL in rails (on the server side) and they are giving me different results. This explains why I cannot decode the encrypted text on the server side and vice-versa on the client side.

Here is how I am doing it:

Client Side (Crypto JS) - Edited

iv = CryptoJS.enc.Base64.parse("kT+uMuPwUk2LH4cFbK0GiA==")

key = CryptoJS.enc.Hex.parse("6476b3f5ec6dcaddb637e9c9654aa687")

encrypted_text = CryptoJS.AES.encrypt("test", key, {mode: CryptoJS.mode.CBC, formatter : Base64Formatter, iv : iv})

encrypted_text => "7Qu7/V7yXHt67wMOV0/1Tg=="

Server Side (Rails OpenSSL) - Edited

iv = Base64.decode64("kT+uMuPwUk2LH4cFbK0GiA==")

key = "6476b3f5ec6dcaddb637e9c9654aa687"

cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')

cipher.encrypt

cipher.key = key

cipher.iv = iv

text = cipher.update("test") + cipher.final

encrypted_text = Base64.strict_encode64(text)

encrypted_text => "fHhNBuopuuthdq2SFvvgDw=="

Does anyone have any idea as to what I am doing wrong? I am just stumped at this point.

Help is much appreciated..thanks!

Paul

like image 845
pwc Avatar asked Oct 07 '22 20:10

pwc


1 Answers

In the line:

key = CryptoJS.enc.Hex.parse("abcdefghijklmnopqrstuvwxyz012345")

The string "abcdefghijklmnopqrstuvwxyz012345" is not in hexadecimal notation. I'd start with that.

like image 164
cytinus Avatar answered Oct 12 '22 11:10

cytinus