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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |