Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created November 5, 2019 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/5bf440a7d7e2b73f8feb7865ad660734 to your computer and use it in GitHub Desktop.
Save havenwood/5bf440a7d7e2b73f8feb7865ad660734 to your computer and use it in GitHub Desktop.
Ruby Example of AES-256 in CBC Mode for consti on #ruby IRC
secret = 'cicada'
key = 'wombat'
cipher = OpenSSL::Cipher::AES256.new :CBC
cipher.encrypt
salt = OpenSSL::Random.random_bytes 16
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1 key, salt, 20000, 32
iv = cipher.random_iv
encrypted = cipher.update(secret) << cipher.final
##
# Then later...
password = 'wombat'
cipher = OpenSSL::Cipher::AES256.new :CBC
cipher.decrypt
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1 password, salt, 20000, 32
cipher.iv = iv
cipher.update(encrypted) << cipher.final
#=> cicada
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment