nickswanjan has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Masters, I have Crypt::RSA::Key::Private and Crypt::RSA::Key::Public keys that have been generated and stored in files. Now I need to export these keys to files in PEM format. I found Convert::PEM, which looks like it might do the job but I can't figure out how to interface it with the keys I have loaded with Crypt::RSA and save the files. Any ideas how to convert the perl hash text format file that Crypt::RSA uses to a PEM? Regards, Nick

Replies are listed 'Best First'.
Re: Converting Crypt::RSA keys to PEM?
by Anonymous Monk on May 07, 2011 at 00:26 UTC
    I would use the openssl commandline tool
      That was my first thought as well, but Crypt::RSA stores the file in its own format that OpenSSL does not handle. If I can get it into a format that OpenSSL can convert, then I am all set. For example, the public key file format looks like this: $VAR1 = bless( { 'e' => 12345, 'n' => '70417859230752389054237849012374890235682758947589662343757682936589068473829473285793281610859206247239036189012957380568307162358317503', 'Version' => '1.0', 'Identity' => 'myname <myname@example.com>' }, 'Crypt::RSA::Key::Public' );
Re: Converting Crypt::RSA keys to PEM?
by nickswanjan (Initiate) on May 12, 2011 at 16:08 UTC
    In case anyone else is looking for a way to convert Crypt::RSA keys, or for a way to generate an RSA key given its integer elements, here is the solution I eventually came up with:
    use Crypt::RSA; use Crypt::OpenSSL::RSA; use Crypt::OpenSSL::Bignum; $key = new Crypt::RSA::Key::Private ( Filename => 'private.key' Password => 'secret-word' ); $n = Crypt::OpenSSL::Bignum->new_from_decimal($key->{private}->{_n}); $e = Crypt::OpenSSL::Bignum->new_from_decimal($key->{private}->{_e}); $d = Crypt::OpenSSL::Bignum->new_from_decimal($key->{private}->{_d}); $p = Crypt::OpenSSL::Bignum->new_from_decimal($key->{private}->{_p}); $q = Crypt::OpenSSL::Bignum->new_from_decimal($key->{private}->{_q}); $rsa = Crypt::OpenSSL::RSA->new_key_from_parameters($n, $e, $d, $p, $q +); print $rsa->get_private_key_string();