Created
November 5, 2019 16:12
-
-
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
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
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