Skip to content

Instantly share code, notes, and snippets.

@pochi
Created February 10, 2017 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pochi/825d9758da7fd9fd4ee0175cc143451c to your computer and use it in GitHub Desktop.
Save pochi/825d9758da7fd9fd4ee0175cc143451c to your computer and use it in GitHub Desktop.
require "openssl"
require "base64"
include Base64
# 0-15: 24
plain_text = "p"*15
# 44
#plain_text = "p"*16
# 44
#plain_text = "p"*31
# 64
#plain_text = "p"*32
# 64
#plain_text = "p"*47
#
#plain_text = "p"*48
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*8)
puts "Key size: " + key.size.to_s
puts "Iv: " + urlsafe_encode64(iv)
puts "Iv size: " + iv.size.to_s
puts "Plain text: " + plain_text
puts "Plain text size: " + plain_text.size.to_s
puts "Cipher text: " + urlsafe_encode64(cipher_text)
puts "Cipher size: " + urlsafe_encode64(cipher_text).size.to_s
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