From: Joshua Mckinney on
Ha..well that would make sense given unpack returns an array. Deductive
reasoning was on the back-burner yesterday.

Made a small change so decrypt returns the string only

The following worked in 1.8.7 and 1.9.1

def mysql_encrypt(s, key)
encrypt(s, mysql_key(key))
end

def mysql_decrypt(s, key)
puts s
decrypt(s, mysql_key(key))
end

protected
def aes(m,k,t)
c = OpenSSL::Cipher::Cipher.new('aes-128-ecb').send(m)
c.key = k
c.update(t) + c.final

end

def encrypt(text, key)
aes(:encrypt, key, text).unpack("H*")[0]
end

def decrypt(text, key)
aes(:decrypt, key, [text].pack("H*"))
end

def mysql_key(key)
final_key = "\0" * 16
key.length.times do |i|
final_key[i%16] = (final_key[i%16].ord ^ key[i].ord).chr
end
final_key
end

Thanks,

Josh

Brian Candler wrote:
> Joshua Mckinney wrote:
>> Awesome, could not find .pack in the 1.9.1 documentation
>
> It's Array#pack, as opposed to String#unpack.

--
Posted via http://www.ruby-forum.com/.