Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: RSA encrypt but no decrypt. What is the problem? (updated)

by haukex (Bishop)
on May 07, 2021 at 08:12 UTC ( #11132203=note: print w/replies, xml ) Need Help??

in reply to RSA encrypt but no decrypt. What is the problem?

It appears to be a problem* with the key you've provided. Adding die "invalid key" unless $rsa->check_key; causes the script to die. The following works for me:

use warnings; use strict; use Crypt::OpenSSL::RSA; use Crypt::OpenSSL::Bignum; use Data::Dump; my $rsa = Crypt::OpenSSL::RSA->generate_key(1024); my $priv = $rsa->get_private_key_string; my $ciphertext = $rsa->encrypt("Hello"); my $rsa2 = Crypt::OpenSSL::RSA->new_private_key($priv); my $plaintext = $rsa2->decrypt($ciphertext); dd $plaintext; my %params; @params{qw/n e d p q/, 'd mod (p-1)', 'd mod (q-1)', '1/q mod p'} = map { $_->to_hex } $rsa->get_key_parameters; dd \%params;

* Update: Specifically, when I replace your d and n parameters with newly generated ones, your code works for me (as long as both sides use pkcs1_padding or pkcs1_oaep_padding, the latter being the default). Note that $rsa->check_key checks for the presence of the parameters n, e, d, p, and q, you're not giving it p and q (though the decryption still works). If you were to provide Crypt::OpenSSL::RSA with only n and e, that would tell the module it's a public key only. I'm not sure why you're working with keys the way you've shown in your post?

Replies are listed 'Best First'.
Re^2: RSA encrypt but no decrypt. What is the problem? (updated)
by Forb (Novice) on May 08, 2021 at 11:14 UTC

    Yes, thanks, I have checked, your code works for me, but it is not convenient for me. In your code, encryption and decryption is done within a single script. In practice, this is not convenient. I would like one script to encrypt the message and another script to decrypt the message. In this case, it is not possible to use Crypt :: OpenSSL :: RSA-> generate_key (1024); in both scripts. The best option is to use the parameters n, e, and d.

      The best option is to use the parameters n, e, and d.

      Personally, I'd use the string representation of the keys, since that's also a standard way of sharing them, but you're free to use the parameters if you wish. My code shows examples of both.

        Two questions:

        1. Does your method mean that in Perl for RSA I must use the generate_key() function to generate keys? Can I do without this function in Perl and set the parameters n, e, d myself? For example, in the C-language, this is possible.

        2. I tried adding one line to your code:

        my $plaintext = $rsa2->public_decrypt($ciphertext);

        but unfortunately this script gives an error: "unknown padding type". Of course, the error can be avoided by adding one more line:


        But as a result, the decryption of the text will not be correct. Do you think why?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11132203]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-07-30 04:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found