Perl中的RSA密钥

前端之家收集整理的这篇文章主要介绍了Perl中的RSA密钥前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有简单的RSA编码/解码脚本,如:
use Crypt::RSA;

my $rsa = new Crypt::RSA;
my($public,$private) = $rsa->keygen( Size => 2048 )
                        or die $rsa->errstr();

my $email = '...';

my $em = $rsa->encrypt( Message => $email,Key => $public  );
my $dm = $rsa->decrypt( Ciphertext => $em,Key => $private );

print "$dm\n";

但是,我想修改此脚本以将RSA密钥用作e,d和n值,例如:

e => 10001
d => 135b03530e94874283f0f0000ffff0001
n => 24000c6c9620886831124848640044901

如何将这三个参数转换为Crypt :: RSA :: Key对象,反之亦然?

或者,如何使用Crypt :: RSA这三个参数进行加密和解密?

解决方法

我建议你检查这个模块: Crypt::OpenSSL::RSA,Crypt::OpenSSL::Bignum,或检查我如何使用它我的存储库在github: Perl backend for Jcryption

通过参数n,e,d创建RSA

use strict;

use Crypt::OpenSSL::Bignum;
use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;

my $param_ref = {
    n => 'C8F19E3A7C18A5FE6D1DE07926F4E6403EA6558EE4892D29155F2C52678E210FE5D61DBC9EC77F2DD39FC8D9F2717F23629B7E18BDB38AADF9FC5BF1CA968C67F4BF0F2D6657E1374A845048B6CDCB2B8B2B3F31BF7F134A11B9A063EB7C326B27C0027955637E6089293F5F4CD40F5B91142BBA61B8E473A8D14B447896B5FD',e => '010001',d => '574C612997703DED630A6FD3329182D6B6611D1A8F3EE957C7E9292043CD03A01C82013948E1F2E6BDE1D5289AA3A450832E7A81D528833F4233D8DE1ADD7CF1878CBACDE64DE4364F8B73093753D6E322732EAEC942281CE4AF55D212E77EC4A108708864D3F86A67C63FBE8206A543A42ABF45E56C1E41A2F2BA0CCC1C8001',};

my $n = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{n});
my $e = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{e});
my $d = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{d});

my $rsa = Crypt::OpenSSL::RSA->new_key_from_parameters($n,$e,$d);

printf "private key is: %s\n",$rsa->get_private_key_string();
printf "public key (in PKCS1 format) is: %s\n",$rsa->get_public_key_string();

从RSA对象获取关键参数

use strict;

use Crypt::OpenSSL::Bignum;
use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;

my $rsa = Crypt::OpenSSL::RSA->generate_key(2048);
my ($n,$d,$p,$q) = $rsa->get_key_parameters();

printf "private key is: %s\n",$rsa->get_public_key_string();
原文链接:https://www.f2er.com/Perl/171521.html

猜你在找的Perl相关文章