Heads up: This description was created by AI and might not be 100% accurate.

with_rsa.rb

This Ruby code snippet demonstrates the generation of an RSA key pair, encryption of a string, and subsequent decryption using the private key. It utilizes the OpenSSL library to perform these cryptographic operations, effectively demonstrating basic RSA key management and encryption.

Ruby code snippet

require 'openssl'
#=> true

rsa_private = OpenSSL::PKey::RSA.generate(2048)
#=> #<OpenSSL::PKey::RSA:0x00007facd0b26720 oid=rsaEncryption>
rsa_public = rsa_private.public_key
#=> #<OpenSSL::PKey::RSA:0x00007facd0b21ec8 oid=rsaEncryption>

secret = 'Ruby'
#=> "Ruby"

enc_data = rsa_public.public_encrypt(secret)
#=> 
"'\xF3\xBB\x88O\a\x8B!\x13\x93z=.\x04\x80p\xA4\xDB\x11\xD5@\x89\x88\xFE\t\xA3\xF...

rsa_private.private_decrypt(enc_data)
#=> "Ruby"

Executed with Ruby 3.4.5.