Search code examples
ruby-on-railsencryptionopensslaescryptojs

sync AES ecnryption between cryptoJS and openSSL (ruby)


I can't get same result when encrypting using CryptoJS or Ruby's OpenSSL

JS code

k=CryptoJS.enc.Hex.parse('ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb');
iv=CryptoJS.enc.Hex.parse('3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d');
r=CryptoJS.AES.encrypt("hello", k, { iv: iv });
alert(r.ciphertext.toString(CryptoJS.enc.Base64));

and in Rails

cipher = OpenSSL::Cipher::AES.new(256, :CBC)
cipher.encrypt
cipher.key = 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb'
cipher.iv = '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d'
enc = cipher.update('hello')+cipher.final
puts Base64.encode64(enc)

the 2 results do not give the same encrypted message.


Solution

  • The IV has to be 16 bytes or 128-bit long, because AES has a blocksize of 128-bit. Your current IV has 32 bytes. So there is probably a difference between how CryptoJS and OpenSSL treat an IV that is too long.