with_rsa.rb
Heads up: This description was created by AI and might not be 100% accurate.
This Ruby code snippet demonstrates RSA encryption and decryption using the openssl library. It generates a 2048-bit RSA key pair (private and public keys). A string “Ruby” is encrypted using the public key and then successfully decrypted using the corresponding private key, showcasing the basic functionality of asymmetric encryption.
Ruby code snippet
require 'openssl'
#=> true
rsa_private = OpenSSL::PKey::RSA.generate(2048)
#=>
#<OpenSSL::PKey::RSA:0x00007f47b0264220 oid=rsaEncryption type_name=RSA provider...
rsa_public = rsa_private.public_key
#=>
#<OpenSSL::PKey::RSA:0x00007f47b029d1b0 oid=rsaEncryption type_name=RSA provider...
secret = 'Ruby'
#=> "Ruby"
enc_data = rsa_public.public_encrypt(secret)
#=>
"eOXB\xB6\xBC\bg\xC3\"\x12|m\xB7\x026Z\xB89tp\xB6rw\xA8\x83!\xFFn6\x98\r\x10\xDF...
rsa_private.private_decrypt(enc_data)
#=> "Ruby"
Executed with Ruby 4.0.1.