Heads up: This description was created by AI and might not be 100% accurate.
with_rsa.rb
This Ruby code snippet demonstrates how to use the OpenSSL library in Ruby to perform RSA encryption and decryption. The code generates a 2048-bit RSA key pair, consisting of a private key and a public key. The private key is used for encryption, while the public key is used for decryption.
The code first creates an instance of the OpenSSL::PKey::RSA
class with the generate
method, passing in the number of bits to use for the RSA key pair. It then extracts the public key from the private key using the public_key
method and stores it in a variable named rsa_public
.
The code then defines a string variable named secret
that contains the message to be encrypted, which is set to “Ruby”.
Next, the code encrypts the message using the public key by calling the public_encrypt
method on the OpenSSL::PKey::RSA
instance, passing in the message to be encrypted. The resulting encrypted data is stored in a variable named enc_data
.
Finally, the code decrypts the message using the private key by calling the private_decrypt
method on the OpenSSL::PKey::RSA
instance, passing in the encrypted data. The result of the decryption is printed to the console.
The output of the program will be “Ruby”, which demonstrates that the encryption and decryption processes were successful.
Ruby code snippet
require 'openssl'
#=> true
rsa_private = OpenSSL::PKey::RSA.generate(2048)
#=> #<OpenSSL::PKey::RSA:0x00007f966c37b518 oid=rsaEncryption>
rsa_public = rsa_private.public_key
#=> #<OpenSSL::PKey::RSA:0x00007f966c376cc0 oid=rsaEncryption>
secret = 'Ruby'
#=> "Ruby"
enc_data = rsa_public.public_encrypt(secret)
#=>
"\x93_\xC2b\xDB\x04\xE2\x99\xE6\\\x03/\x94\xB7\x96\r\x8D\e\x1E\x9DD\x19\xD5\xE7\...
rsa_private.private_decrypt(enc_data)
#=> "Ruby"
Executed with Ruby 3.4.4
.