Take the 2-minute tour ×

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)

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

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Yes, you would just do:

return (@key == @private_key.private_decrypt(crypt_key)) rescue false
share|improve this answer
Thanks, works now, hope that the error is only related to fake .pem and ignore him don't affect security... –  byterussian Nov 6 '12 at 12:44
add comment

Your Answer


