Skip to content

Instantly share code, notes, and snippets.

@eaconde
Last active April 18, 2022 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eaconde/fd5f02c3a1447417de8ea933d8fc3d01 to your computer and use it in GitHub Desktop.
Save eaconde/fd5f02c3a1447417de8ea933d8fc3d01 to your computer and use it in GitHub Desktop.
# encrypt.rb
#!/usr/bin/env ruby
require 'openssl'
require 'base64'
public_key_file = 'public.pem';
string = 'Hello World!';
public_key =
OpenSSL::PKey::RSA.new(File.read(public_key_file))
encrypted_string =
Base64.encode64(public_key.public_encrypt(string))
print encrypted_string, "\n"
---
# decrypt.rb
#!/usr/bin/env ruby
require 'openssl'
require 'base64'
private_key_file = 'private.pem';
password = 'boost facile'
encrypted_string = %Q{
qBF3gjF8iKhDh+g+TOvAzBkJA/1d2lD8RUyz2Ol+s1OpLB5aA3RA7EHm0KGL
XaP3upvJ7I5rN1yO9Qat9kyRQu9OMqAUmFvwUaiW/1NPjxnpmcFn9mhkttP9
qfO6iIfyxErUqKIxHYqavyPmivre9eEcXiBdtIK6NJJKG3WmSfIFgpZ6eBWI
wxlZg+x0fI4L2JsODMGx5Khn7CUt0bTkH6HMHwxEG24NbsmrqtC2zn8Hm/87
UyN5ZCDyJ/mtIHAjzPry6vbVPTF0QCR4lZ7uSt/W7JZ0tNgX7eQQwoPCgbqU
/uwRCwww/c407jw7YEE5Lgpx20/jyLXJwvZHxNEcxA==
}
private_key =
OpenSSL::PKey::RSA.new(File.read(private_key_file),password)
string =
private_key.private_decrypt(Base64.decode64(encrypted_string))
print string, "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment