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

Hello

On Windows, I'm using Crypt::PK::RSA to generate key pairs like this:

use Crypt::PK::RSA; my $pk = Crypt::PK::RSA->new(); $pk->generate_key(256, 65537); my $private_pem = $pk->export_key_pem('private'); my $public_pem = $pk->export_key_pem('public');

Private key format is fine. Public key is generated like this:

-----BEGIN RSA PUBLIC KEY----- ... -----END RSA PUBLIC KEY-----

Since I need to add this key to authorized_keys files it needs to be in a ssh-rsa format.

Can easly be done using ssh-keygen to convert:

$ ssh-keygen -y -f privatekey > publickey

I found this thread explaining the process and having several solutions none perl based.

And all end up being dependent on ssh-keygen or open-ssl both not natively available on Windows

Does anyone know how to do this only in perl or if it is even possible?

Replies are listed 'Best First'.
Re: Convert pem to ssh-rsa
by hippo (Archbishop) on Aug 23, 2019 at 12:49 UTC
    Since I need to add this key to authorized_keys files it needs to be in a ssh-rsa format

    I've not used it myself, but perhaps Net::SSH::Perl::Key->dump_public might do the trick? Just a suggestion.

      Hello hippo, That worked! Thanks a million.
      use Crypt::PK::RSA; my $pk = Crypt::PK::RSA->new(); $pk->generate_key(256, 65537); my $private_pem = $pk->export_key_pem('private'); my $public_pem = $pk->export_key_pem('public'); use Net::SSH::Perl::Key; my $key = Net::SSH::Perl::Key->read_private('RSA', \$private_pem); say $key->dump_public;

      Output:

      ssh-rsa AAAAB...