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?


In reply to Re: 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.