Skip to content

Instantly share code, notes, and snippets.

@emboss
Created December 15, 2011 10:57
Show Gist options
  • Save emboss/1480700 to your computer and use it in GitHub Desktop.
Save emboss/1480700 to your computer and use it in GitHub Desktop.
What's the default IV for Cipher?
require 'openssl'
data = "lesecret" * 10
cipher = OpenSSL::Cipher::AES256.new("CBC")
key = OpenSSL::Random.random_bytes(cipher.key_len)
cipher.encrypt
cipher.key = key
enc = cipher.update(data) + cipher.final
dec = OpenSSL::Cipher::AES256.new("CBC")
dec.decrypt
dec.key = key
dec.iv = "\0" * cipher.iv_len #this is the default IV, implied by OpenSSL itself
text = dec.update(enc) + dec.final
puts text == data # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment