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:0x00007f62d2df0818 oid=rsaEncryption type_name=RSA provider...
rsa_public = rsa_private.public_key
#=> #<OpenSSL::PKey::RSA:0x00007f62d2ee78e8 oid=rsaEncryption type_name=RSA provider...
secret = 'Ruby'
#=> "Ruby"
enc_data = rsa_public.public_encrypt(secret)
#=> "\x97\x8Ek\xFD\x13\x98\xA0\x05\xE6\x148\xB3\x7F1\xCA\x7FH\xF2?\x12\xD2\x8DW\x80|...
rsa_private.private_decrypt(enc_data)
#=> "Ruby"
Ruby 4.0.5