Take the 2-minute tour ×

I'm trying to decrypt an RSA encrypted message using the public key.

Using Crypt::OpenSSL::RSA, I am able to encrypt using either key, but I can only decrypt with the private key. Attempting to decrypt with the public key:

use Crypt::OpenSSL::RSA;
use MIME::Base64;
use File::Slurp;

my $public_key  = 'rsa.pub.pem';
#my $private_key = 'rsa.priv.pem';
my $rsa_public  = Crypt::OpenSSL::RSA->new_public_key(scalar read_file $public_key);
#my $rsa_private = Crypt::OpenSSL::RSA->new_private_key(scalar read_file $private_key);

my $ciphertext_b64 = 'cqyPNNfqYaUeIsM1yAz7IsQ760Bkd4IPaatHnMQtQAMKtYTEUqFHwnSZ4hg2

my $ciphertext = decode_base64($ciphertext_b64);

print $rsa_public->decrypt($ciphertext);
#print $rsa_private->decrypt($ciphertext);

Results in: Public keys cannot decrypt at test.pl line 19.

Incidentally, Ruby seems to have no problem encrypting and decrypting with either key (which is why I'm currently in this situation).

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Did you realize there is a public_decrypt method in Crypt::OpenSSL::RSA?

share|improve this answer
I'm an idiot. Thanks :) –  aidan Oct 16 '12 at 22:37
add comment

You should either decrypt with the private key or verify a signature with the public key and use appropriate methods for doing so. It is unclear from your question whether you do encryption or signatures.

If you try to "decrypt an RSA encrypted message" then it is quite unclear what you want to do and what the library you are using is actually doing. The paddings that are used for encryption and digital signatures are different for RSA. Hence if you call the wrong methods then in the best case you get an error message and in the worst case you end up with an insecure scheme.

share|improve this answer
I would agree with you but at some point it becomes an argument over terminology. The API provides low-level private_encrypt and public_decrypt` methods as well as the more usual and slightly higher-level sign and verify methods. –  GregS Oct 17 '12 at 17:00
@GregS I'm certainly aware of private_encrypt. But did you notice the print statement in the code snippet? There is a good chance that the OP may not want to verify a signature. Hence the first thing to do would clarify the goal of the application. Recommending a rather low-level function seems wrong to me. –  Jack Oct 18 '12 at 8:38
add comment

Your Answer


