我正在尝试使用公钥解密 RSA 加密消息。
使用Crypt::OpenSSL::RSA
,我可以使用任一密钥进行加密,但我只能使用私钥解密。尝试使用公钥解密:
use Crypt::OpenSSL::RSA;
use MIME::Base64;
use File::Slurp;
my $public_key = 'rsa.pub.pem';
#my $private_key = 'rsa.priv.pem';
my $rsa_public = Crypt::OpenSSL::RSA->new_public_key(scalar read_file $public_key);
#my $rsa_private = Crypt::OpenSSL::RSA->new_private_key(scalar read_file $private_key);
my $ciphertext_b64 = 'cqyPNNfqYaUeIsM1yAz7IsQ760Bkd4IPaatHnMQtQAMKtYTEUqFHwnSZ4hg2
pkoJM1N5Ejlv6Eqkk/ZaMWl1nTDOxRDj0V6PARQPqz3QF1UGWkSMxMt/DlSn
AtrRXgjvrILbMX5BsV2S5mHcLoCeNVb+jdnX0x0Uu/AAFPsByPRrt1yM1ORo
KcP+0ENvcvJ8yGOxJ2jOEmTFkQM5kjNDIFmLUlt6qODdTGWvYWR2CDduLO4m
qiyAt4yK5K3vwMybAG5ceRGb/kmMSW10EnvbryIdDGVGS8Zvodu3xqtbM1Yo
tdtZRDkcUcOYlUi3VRvSTimatVkJPG8QDlZofrBA0w==';
my $ciphertext = decode_base64($ciphertext_b64);
print $rsa_public->decrypt($ciphertext);
#print $rsa_private->decrypt($ciphertext);
结果是:Public keys cannot decrypt at test.pl line 19.
顺便说一句,Ruby 似乎使用任一密钥加密和解密都没有问题(这就是我目前处于这种情况的原因)。