Created
July 5, 2011 19:55
-
-
Save listrophy/1065746 to your computer and use it in GitHub Desktop.
Using AES in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
require 'base64' | |
secret_message = 'foo!' | |
aes = OpenSSL::Cipher.new 'AES-256-CBC' | |
aes.encrypt | |
iv = aes.random_iv # never EVER use the same IV if you're reusing a key | |
rnd = aes.random_key | |
puts "IV size: #{iv.length * 8}" | |
puts "Key: #{Base64.strict_encode64 rnd}" | |
puts "IV: #{Base64.strict_encode64 iv}" | |
res = aes.update(secret_message) + aes.final | |
puts "Encrypted, base64'ed: #{Base64.strict_encode64(res)}" | |
dec = OpenSSL::Cipher.new 'AES-256-CBC' | |
dec.decrypt | |
dec.key = rnd | |
dec.iv = iv | |
res2 = dec.update(res) + dec.final | |
puts "Decrypted: #{res2}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment