With_rsa.rb

This content was produced by an LLM and could include errors.

This script demonstrates RSA encryption. It generates a key pair, encrypts a secret message using the public key, and successfully decrypts the data using the corresponding private key.

require 'openssl'
#=> true

rsa_private = OpenSSL::PKey::RSA.generate(2048)
#=> #<OpenSSL::PKey::RSA:0x00007fc0abda65b0 oid=rsaEncryption type_name=RSA provider...
rsa_public = rsa_private.public_key
#=> #<OpenSSL::PKey::RSA:0x00007fc0abda22f8 oid=rsaEncryption type_name=RSA provider...

secret = 'Ruby'
#=> "Ruby"

enc_data = rsa_public.public_encrypt(secret)
#=> "\x15.6~\xEC\xBF\xB6\x81\x03=N\x93?_\x11\xEF\x87\xED&\xCE \xA8rU\x92\xBA\r\x8Dh\...

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

Ruby 4.0.3