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:0x00007f26f13e0638 oid=rsaEncryption type_name=RSA provider...
rsa_public = rsa_private.public_key
#=> #<OpenSSL::PKey::RSA:0x00007f26f14d7168 oid=rsaEncryption type_name=RSA provider...
secret = 'Ruby'
#=> "Ruby"
enc_data = rsa_public.public_encrypt(secret)
#=> "F\xC0a\xE0\xD3\xD3D_R\\\xE0\xB9\xE2\x9E\xE8+\xE2\xD1\xE4p\xD2\xA3\xD5\xF3\xF8Z\...
rsa_private.private_decrypt(enc_data)
#=> "Ruby"
Ruby 4.0.5