Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created June 2, 2011 03:09
Show Gist options
  • Select an option

  • Save AquaGeek/1003855 to your computer and use it in GitHub Desktop.

Select an option

Save AquaGeek/1003855 to your computer and use it in GitHub Desktop.
OpenSSL RSA Keygen and Encryption/Decryption
# From: http://snippets.dzone.com/posts/show/3029
# .generate creates an object containing both keys
new_key = OpenSSL::PKey::RSA.generate( 1024 )
puts "Does the generated key object have the public key? #{new_key.public?}\n"
puts "Does the generated key object have the private key? #{new_key.private?}\n\n"
# write the new keys as PEM's
new_public = new_key.public_key
puts "New public key pem:\n#{new_public}\n"
puts "The new public key in human readable form:\n"
puts new_public.to_text + "\n"
output_public = File.new("./new_public.pem", "w")
output_public.puts new_public
output_public.close
new_private = new_key.to_pem
puts "new private key pem:\n#{new_private}\n"
output_private = File.new("./new_private.pem", "w")
output_private.puts new_private
output_private.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment