with_rsa.rb(encryption)
- Generate RSA Key Pair:
- Import OpenSSL library.
- Generate a 2048-bit RSA private key.
- Derive the corresponding public key.
- Encrypt Data with RSA:
- Use the RSA public key to encrypt the string ‘Ruby’.
- Produce encrypted data (
enc_data
).
- Decrypt Data with RSA:
- Employ the RSA private key to decrypt the previously encrypted data (
enc_data
). - Retrieve the original string (‘Ruby’).
- Employ the RSA private key to decrypt the previously encrypted data (
These snippets showcase RSA key generation, encryption, and decryption using the OpenSSL library in Ruby.
Execution:
require 'openssl'
#=> true
rsa_private = OpenSSL::PKey::RSA.generate(2048)
#=> #<OpenSSL::PKey::RSA:0x00007fa12f09b0f0 oid=rsaEncryption>
rsa_public = rsa_private.public_key
#=> #<OpenSSL::PKey::RSA:0x00007fa12f0ad9f8 oid=rsaEncryption>
secret = 'Ruby'
#=> "Ruby"
enc_data = rsa_public.public_encrypt(secret)
#=> "\x8E\x1C&\xED\xEC\xD2\xDF\x8F\xE4\x02\x00\xA3v\xE8\x99fz\x1A\x80\b\xE4\...
rsa_private.private_decrypt(enc_data)
#=> "Ruby"
Executed with Ruby 3.4.2