Convert .pem key file to .ppk in Windows automatically/script/command line

laffoyb picture laffoyb · Apr 15, 2015 · Viewed 7.9k times · Source

I'm using Vagrant to create VMs on a Windows host, to which I would like to connect with PuTTY. Vagrant creates an RSA private key in the .pem format. PuTTY needs a key in the .ppk format to create a connection.

I would like to convert the .pem to .ppk automatically when creating the vagrant VM.

The question of how to convert .pem to .ppk has been asked and answered lots of times, but on Windows all those answers involve clicking through the puttygen GUI. It seems that on Linux, puttygen can be operated entirely from the command line, but on Windows the GUI must be used.

Having to click through a GUI is a slow point in my workflow when creating new VMs that I would like to avoid.

Is there any command-line/scriptable/programmatic way of converting .pem files to .ppk format on Windows?

Answer

Martin Prikryl picture Martin Prikryl · Apr 15, 2015

WinSCP supports command-line conversion of private keys from the OpenSSH (or ssh.com) format to the PuTTY .ppk format.

Use the /keygen switch:

winscp.com /keygen mykey.pem /output=mykey.ppk

(I'm the author of WinSCP)


Or, you can compile/run Unix command-line puttygen using Cygwin.


Or build your own tool from PuTTY code, it's open-source. It is rather easy (that's basically what WinSCP does).

Use import_ssh2 to load the .pem:

ssh2_userkey *import_ssh2(const Filename *filename, int type,
                          char *passphrase, const char **errmsg_p);

Use ssh2_save_userkey to save it as .ppk:

bool ssh2_save_userkey(
    const Filename *filename, ssh2_userkey *key, char *passphrase);