公開鍵を用いた暗号化、秘密鍵を用いた復号化テスト用rubyスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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