Skip to content

Instantly share code, notes, and snippets.

@rwjblue
Last active March 16, 2016 16:18
Show Gist options
  • Save rwjblue/5922584 to your computer and use it in GitHub Desktop.
Save rwjblue/5922584 to your computer and use it in GitHub Desktop.
Public-key encryption with Ruby.
require 'openssl'
require 'base64'
ciphertext = Base64.decode64("CIPHERTEXT GOES HERE")
passphrase = ENV['PRIVATE_KEY_PASSPHRASE'] # read in from environment
private_key = OpenSSL::PKey::RSA.new(File.read('path/to/private_key.pem'), passphrase)
plaintext = private_key.private_decrypt(ciphertext, OpenSSL::PKey::RSA::PKCS1_PADDING)
puts "Plain text is: " + plaintext
require 'openssl'
require 'base64'
public_key = OpenSSL::PKey::RSA.new(File.read('path/to/public_key.pem'), '')
ciphertext = public_key.public_encrypt(plaintext, OpenSSL::PKey::RSA::PKCS1_PADDING)
ciphertext = Base64.encode64(ciphertext)
puts "Encrypted text is: " + ciphertext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment