【问题标题】:Ruby OpenSSL error when try to decrypt a key with a fake RSA private key尝试使用假 RSA 私钥解密密钥时出现 Ruby OpenSSL 错误
【发布时间】:2012-11-06 12:30:27
【问题描述】:

有这个 Ruby 代码:

require 'openssl'
require 'securerandom'

class AuthRsa

def initialize(public_key, private_key)

  @public_key = OpenSSL::PKey::RSA.new(public_key)
  @private_key = OpenSSL::PKey::RSA.new(private_key)
  @key = SecureRandom.base64(16)
end

def valid?
  crypt_key = @public_key.public_encrypt(@key)
  return @key == @private_key.private_decrypt(crypt_key)
end
end

使用正确的公钥和私钥测试类时一切正常,但提交假证书时测试失败并出现以下错误:OpenSSL::PKey::RSA Error: padding check failed

是否可以将方法 private_decrypt 设置为返回 false 而不是错误?

【问题讨论】:

    标签: ruby authentication openssl rsa


    【解决方案1】:

    是的,你会这样做:

    return (@key == @private_key.private_decrypt(crypt_key)) rescue false
    

    【讨论】:

    • 谢谢,现在可以工作了,希望错误只和假的.pem有关,忽略他不要影响安全...