26

I have generated a key pair with ssh-keygen. I now want to use the same key pair with PuTTY-based applications.

Because I'm working in a CLI environment, I want to convert the key to the .ppk format from within this environment.

Apparently, you can use puttygen openssh_private_key -o output_filename.ppk on UNIX-like environments. But the Windows version of puttygen will just load the key and prompt you to use the GUI to export the key.

Batch mode for Windows PuTTYgen suggests that this simply is not currently possible, at least with puttygen. I'd gladly accept and option that uses other means though.

How can I convert the key from the command line?

30

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 the Unix command-line puttygen using WSL or Cygwin.


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

Use the import_ssh2 to load the .pem:

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

Use the ssh2_save_userkey to save it as the .ppk:

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

NOTE: The OP requires Command line. This answer uses a graphical application... Will leave the answer here just in case it is useful.

There is a possibly simpler solution.

1) Download (if you do not already have it) the PuTTY Key Generator (PuTTYgen) application by going here or here.

2) Open PuTTYgen

3) Click Conversions, then click import key

4) Locate your key file, then click open.

You can now save your key as a PPK file by clicking the Save private key button

You can also save a public version of your key by clicking the Save public key button

https://devops.profitbricks.com/tutorials/use-ssh-keys-with-putty-on-windows/

https://kb.site5.com/shell-access-ssh/how-to-convert-ssh-keys-to-ppk-format/

2
  • 2
    The question is about "Windows command line". Dec 19 '17 at 12:48
  • @MartinPrikryl - You are correct, I will make a note on it. I could as well remove it, but I have seen very often similar cases where readers judged answers useful anyway. And this is the main goal of SO. Dec 19 '17 at 15:51
7

If you're running on linux or generating your key there (or maybe you can?) then you can use

puttygen -t rsa -C "my home key" -o mykey.ppk

If you don't have puttygen already, then sudo apt install putty-tools or the appropriate command on your system to install it.

Otherwise if you're on windows I can't see how you can actually do it, other than generating the key on another machine.

3
  • 1
    This is what I use so far but one annoyance is that in Windows at least, it leaves the puttygen window open, havent found a way to automatically close it
    – Purefan
    Jan 30 '17 at 8:59
  • 1
    Best answer. No manual downloads, just sudo apt install putty-tools and then the above puttygen command, and you're done. You can put the path to the original key instead of "my home key". Sep 4 '19 at 21:08
  • The best answer? OP is asking for conversion not creating an rsa key in ppk format. Most people upvoting because they found this helpful to them in another case but clearly this answer did not follow OP request.
    – Kalib Zen
    Apr 19 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.