Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Convert pem to ssh-rsa

by jpsaraiva (Novice)
on Aug 23, 2019 at 08:06 UTC ( #11104892=perlquestion: print w/replies, xml ) Need Help??

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 (Chancellor) on Aug 23, 2019 at 08: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...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11104892]
Approved by haukex
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2019-09-17 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (206 votes). Check out past polls.

    Notices?