require 'openssl' require 'digest/sha1' c = OpenSSL::Cipher::Cipher.new("aes-256-cbc") c.encrypt # your pass is what is used to encrypt/decrypt c.key = key = Digest::SHA1.hexdigest("yourpass") c.iv = iv = c.random_iv e = c.update("crypt this") e << c.final puts "encrypted: #{e}\n" c = OpenSSL::Cipher::Cipher.new("aes-256-cbc") c.decrypt c.key = key c.iv = iv d = c.update(e) d << c.final puts "decrypted: #{d}\n"
provided by menik from #rubyonrails
c.key = key = Digest::SHA1.hexdigest("yourpass").unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
OpenSSL expects the key in binary format. If you use the command line to encrypt something with the same key, you will notice different results unless you do the above.
If you are using 128 bit, change the 32 to 16. If you are using 196 bit, try 24. (This is the number of bytes to convert from hex to binary. 128/8 = 16, 256/8=32, 196/8=24)
I'm willing to bet that the initialization vector will need the same kind of tweak if you set your own iv.