Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
公開鍵を用いた暗号化、秘密鍵を用いた復号化テスト用rubyスクリプト
require 'openssl'
require 'base64'
# openssl genrsa -out ./id_rsa で作成
private_key = nil
File.open("id_rsa") do |f|
private_key = OpenSSL::PKey::RSA.new(f)
end
# openssl rsa -in id_rsa -pubout -out id_rsa_pub で作成
public_key = nil
File.open("id_rsa_pub") do |f|
public_key = OpenSSL::PKey::RSA.new(f)
end
str = "aaabbbcccdddeeefffhogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
encrypted_str = Base64.encode64(public_key.public_encrypt(str)).gsub("\n", "")
puts encrypted_str
p private_key.private_decrypt(Base64.decode64(encrypted_str))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment