RSA decryption using * public * key in Perl

I am trying to decrypt an encrypted RSA message using a public key.

Using Crypt::OpenSSL::RSA , I can encrypt using any of the keys, but I can only decrypt using the private key. Attempt to decrypt with a 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.

By the way, Ruby does not seem to have problems encrypting and decrypting with any key (which is why I am in this situation now).

0
source share
2 answers

Do you understand that there is a public_decrypt method in Crypt :: OpenSSL :: RSA ?

+4
source

You must either decrypt using the private key, or verify the signature with the public key and use the appropriate methods to do this. It is not clear from your question whether you are performing encryption or signatures.

If you try to "decrypt the encrypted RSA message", then it is completely unclear what you want to do and what the library that you use is using. Gaskets that are used for encryption and digital signatures are different for RSA. Therefore, if you call the wrong methods, then at best you will get an error message, and at worst you will get an unsafe circuit.

0
source

All Articles