Last active
May 16, 2021 19:55
-
-
Save smazhara/4740692 to your computer and use it in GitHub Desktop.
Ruby AES128 encrypt-decrypt example
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" | |
include Base64 | |
plain_text = "The beatings will continue until morale improves" | |
cipher = OpenSSL::Cipher::AES128.new(:CBC) | |
cipher.encrypt | |
key = cipher.random_key | |
iv = cipher.random_iv | |
cipher_text = cipher.update(plain_text) + cipher.final | |
cipher = OpenSSL::Cipher::AES128.new(:CBC) | |
cipher.decrypt | |
cipher.key = key | |
cipher.iv = iv | |
decrypted_plain_text = cipher.update(cipher_text) + cipher.final | |
puts "AES128 in CBC mode" | |
puts "Key: " + urlsafe_encode64(key) | |
puts "Iv: " + urlsafe_encode64(iv) | |
puts "Plain text: " + plain_text | |
puts "Cipher text: " + urlsafe_encode64(cipher_text) | |
puts "Decrypted plain text: " + decrypted_plain_text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment