The problem is that Ruby expects the key to be in binary format, not hex. So you need to do this:
#!/usr/bin/env ruby require 'openssl' require 'base64' data = "When in Rome do as the Romans do" key = "2e35f242a46d67eeb74aabc37d5e5d05" aes = OpenSSL::Cipher.new("aes-128-cbc") aes.encrypt() aes.key = key.scan(/../).collect{|x| x.hex}.pack("c*") iv_value = aes.random_iv aes.iv = iv_value crypt = aes.update(data) + aes.final() crypt_string = (Base64.encode64(iv_value + crypt)) puts crypt_string
For me that prints
mdnLCY6MdwEONY1AxR/vjVKMssB+yrPsz4QMjfl6fDXxv68E0EUxtAqa4VUo fTkjq2Hqyd48UV3dyWmEbwXw5Q==
If I put this in your HTML file (without changing any code), I will return the original message:
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script> <script> data = "mdnLCY6MdwEONY1AxR/vjVKMssB+yrPsz4QMjfl6fDXxv68E0EUxtAqa4VUo\nfTkjq2Hqyd48UV3dyWmEbwXw5Q==\n"; key = "2e35f242a46d67eeb74aabc37d5e5d05"; var rawData = atob(data); var iv = rawData.substring(0,16); var crypttext = rawData.substring(16); </script> </head> <body> </body> </html>
So the problem is that your Ruby code was creating a flexible cyphertext. Correct the key and re-encrypt, and JS should start working.
source share