0

有这个 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 而不是错误?

4

1 回答 1

1

是的,你会这样做:

return (@key == @private_key.private_decrypt(crypt_key)) rescue false
于 2012-11-06T12:33:11.067 回答