byterussian
byterussian

Reputation: 3609

Ruby OpenSSL error when try to decrypt a key with a fake RSA private key

Have this Ruby code:

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

When test the class with correct public and private keys is all ok, but when submit a fake certificate the test fail with this error: OpenSSL::PKey::RSA Error: padding check failed

Is possible to set the method private_decrypt to return false instead of an error?

Upvotes: 0

Views: 1766

Answers (1)

pguardiario
pguardiario

Reputation: 55002

Yes, you would just do:

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

Upvotes: 1

Related Questions