需要一个有效的 Ruby 基本 RSA Private Public 密钥示例
Need a working Ruby basic RSA Private Public key example
我已经能够成功地创建私钥和 public 密钥,并对消息进行编码,但在解码消息时出现错误
到目前为止我的代码是:
require 'openssl'
require 'base64'
key = OpenSSL::PKey::RSA.generate(2048)
pri_key = key
pub_key = key.public_key
string = 'Hello World!';
rsa_public_key = OpenSSL::PKey::RSA.new(pub_key)
encrypted_string = Base64.encode64(rsa_public_key.public_encrypt(string))
puts "Encrypted Message:"
puts encrypted_string
# This creates an error
my_string = pri_key.private_decrypt(encrypted_string)
puts "The decoded message"
puts my_string
打印编码消息后抛出此错误
Example Decrypt.txt:25:in `private_decrypt': data greater than mod len (OpenSSL::PKey::RSAError)
from Example Decrypt.txt:25:in `<main>'
你对加密后的字符串进行了base 64编码,但在解密之前没有对其进行解码。由于 base 64 编码字符串比加密字符串长,并且比模数长,所以会出现错误。
加密前尝试 base 64 解码:
my_string = pri_key.private_decrypt(Base64.decode64(encrypted_string))
我已经能够成功地创建私钥和 public 密钥,并对消息进行编码,但在解码消息时出现错误
到目前为止我的代码是:
require 'openssl'
require 'base64'
key = OpenSSL::PKey::RSA.generate(2048)
pri_key = key
pub_key = key.public_key
string = 'Hello World!';
rsa_public_key = OpenSSL::PKey::RSA.new(pub_key)
encrypted_string = Base64.encode64(rsa_public_key.public_encrypt(string))
puts "Encrypted Message:"
puts encrypted_string
# This creates an error
my_string = pri_key.private_decrypt(encrypted_string)
puts "The decoded message"
puts my_string
打印编码消息后抛出此错误
Example Decrypt.txt:25:in `private_decrypt': data greater than mod len (OpenSSL::PKey::RSAError)
from Example Decrypt.txt:25:in `<main>'
你对加密后的字符串进行了base 64编码,但在解密之前没有对其进行解码。由于 base 64 编码字符串比加密字符串长,并且比模数长,所以会出现错误。
加密前尝试 base 64 解码:
my_string = pri_key.private_decrypt(Base64.decode64(encrypted_string))