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.


In reply to Re^5: RSA encrypt but no decrypt. What is the problem? (updated) by haukex
in thread RSA encrypt but no decrypt. What is the problem? by Forb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.