Skip to content

Instantly share code, notes, and snippets.

@smazhara
Created February 8, 2013 18:54
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 smazhara/4741102 to your computer and use it in GitHub Desktop.
Save smazhara/4741102 to your computer and use it in GitHub Desktop.
require "openssl"
require "base64"
include Base64
plain_text = "The beatings will continue until morale improves"
seckey = OpenSSL::PKey::RSA.new(1024)
pubkey = seckey.public_key
# private encryption - public decryption
seckey_cipher_text = seckey.private_encrypt(plain_text)
pubkey_plain_text = pubkey.public_decrypt(seckey_cipher_text)
# public encryption - private decryption
pubkey_cipher_text = pubkey.public_encrypt(plain_text)
seckey_plain_text = seckey.private_decrypt(pubkey_cipher_text)
puts "Seckey: \n" + seckey.to_pem
puts "Pubkey: \n" + pubkey.to_pem
puts "Plain text: " + plain_text
puts "Seckey encrypted cipher text: " + urlsafe_encode64(seckey_cipher_text)
puts "Pubkey decrypted plain text: " + pubkey_plain_text
puts "Pubkey encrypted cipher text: " + urlsafe_encode64(pubkey_cipher_text)
puts "Seckey decrypted plain text: " + seckey_plain_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment