Quick reference for efficient coding
enc_data
).enc_data
).These snippets showcase RSA key generation, encryption, and decryption using the OpenSSL library in Ruby.
require 'openssl'
#=> true
rsa_private = OpenSSL::PKey::RSA.generate(2048)
#=> #<OpenSSL::PKey::RSA:0x00007f820da44098 oid=rsaEncryption>
rsa_public = rsa_private.public_key
#=> #<OpenSSL::PKey::RSA:0x00007f820db3f9c0 oid=rsaEncryption>
secret = 'Ruby'
#=> "Ruby"
enc_data = rsa_public.public_encrypt(secret)
#=> "\xA5\xBDH\x9F\x88:\xA2\xC2$\rEN\x93\x00M32K\\\x17\x87\x9D;k\x01\xA9\xE6...
rsa_private.private_decrypt(enc_data)
#=> "Ruby"
Executed with Ruby 3.3.5