So, I’ve got an app at work that encrypts a string using ColdFusion. ColdFusion’s bulit-in encryption helpers make it pretty simple:
encrypt('string_to_encrypt','key','AES','HEX')
What I’m trying to do is use Ruby to create the same encrypted string as this ColdFusion script is creating. Unfortunately encryption is the most confusing computer science subject known to man.
I found a couple helper methods that use the openssl library and give you a really simple encryption/decryption method. Here’s the resulting string:
"\370\354D\020\357A\227\377\261G\333\314\204\361\277\250"
Which looks unicode-ish to me. I’ve tried several libraries to convert this to hex but they all say it contains invalid characters. Trying to unpack it results in this:
string = "\370\354D\020\357A\227\377\261G\333\314\204\361\277\250"
string.unpack('U')
ArgumentError: malformed UTF-8 character
from (irb):19:in `unpack'
from (irb):19
At the end of the day it’s supposed to look like this (the output of the ColdFusion encrypt method):
F8E91A689565ED24541D2A0109F201EF
Of course that’s assuming that all the padding, initialization vectors, salts, cypher types and a million other possible differences all line up.
Here’s the simple script I’m using to encrypt/decrypt:
def aes(m,k,t)
(aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = Digest::SHA256.digest(k)
aes.update(t) << aes.final
end
def encrypt(key, text)
aes(:encrypt, key, text)
end
def decrypt(key, text)
aes(:decrypt, key, text)
end
Any help? Maybe just a simple option I can pass to OpenSSL::Cipher::Cipher that will tell it to hex-encode the final string?
I faced similar issue today. Mine was a bit trickier, b/c I didn’t have access to CF code – only text to encrypt, key and SHA256 of encrypted result.
Official documentation for Encrypt function says:
In my case I was provided with a 32 chars long MD5 of some string which was a key I had to use.
Since we can not directly use own key for AES ancryption – I had to convert it to the same format
GenerateSecretKeyhas.After some digging it appeared that
GenerateSecretKeycreates random 16 bytes long binary string and encodes it to Base64. But – this is important – decodes it back internally during encryption process.So this is working solution:
CF code:
Ruby code:
Aes module code: