Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: RSA encrypt but no decrypt. What is the problem?

by haukex (Archbishop)
on May 08, 2021 at 17:54 UTC ( [id://11132279]=note: print w/replies, xml ) Need Help??


in reply to Re^2: RSA encrypt but no decrypt. What is the problem? (updated)
in thread RSA encrypt but no decrypt. What is the problem?

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.

  • Comment on Re^3: RSA encrypt but no decrypt. What is the problem?

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

    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:

    $rsa2->use_no_padding();

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

      Can I do without this function in Perl and set the parameters n, e, d myself?

      Yes, my code outputs these parameters and like I said, when I used them in your code, it worked, meaning the values of your parameters appear to be incorrect, but otherwise initializing the keys like you did basically works.

      Does your method mean that in Perl for RSA I must use the generate_key() function to generate keys?

      No, there are plenty of other ways to generate RSA keys. Though I can't test right now to confirm, you should be able to generate an RSA key with openssl-genrsa and get its paramaters with openssl-rsa's -text option*. But again, the PEM representation of keys (-----BEGIN RSA PRIVATE KEY----- and so on) would be my choice.

      I tried adding one line to your code: my $plaintext = $rsa2->public_decrypt($ciphertext); but unfortunately this script gives an error

      That a message enrypted with a public key needs to be decrypted with the matching private key is a pretty essential fact of Public-key cryptography, so you probably want to read up on that. Perhaps you're thinking of Symmetric-key algorithms instead? RSA is not one of those. Maybe have a look at e.g. Crypt::Cipher::AES?

      Minor edit for clarification in first paragraph.

      * Update 2: Confirmed: openssl genrsa 512 | openssl rsa -text -noout gives the following values: modulus=n, publicExponent=e, privateExponent=d, prime1=p, prime2=q. Of course you probably want to use more than 512 bits, but I just did that to be in line with your example in the root node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-24 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found