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
pkoJM1N5Ejlv6Eqkk/ZaMWl1nTDOxRDj0V6PARQPqz3QF1UGWkSMxMt/DlSn
AtrRXgjvrILbMX5BsV2S5mHcLoCeNVb+jdnX0x0Uu/AAFPsByPRrt1yM1ORo
KcP+0ENvcvJ8yGOxJ2jOEmTFkQM5kjNDIFmLUlt6qODdTGWvYWR2CDduLO4m
qiyAt4yK5K3vwMybAG5ceRGb/kmMSW10EnvbryIdDGVGS8Zvodu3xqtbM1Yo
tdtZRDkcUcOYlUi3VRvSTimatVkJPG8QDlZofrBA0w==';
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).
Did you realize there is a
public_decryptmethod in Crypt::OpenSSL::RSA?