38

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?

3 Answers 3

38

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 ppk_save_f to save it as the .ppk:

bool ppk_save_f(const Filename *filename, ssh2_userkey *key,
                const char *passphrase, const ppk_save_parameters *params)
3
  • Does winscp.com still have this functionality, or is it potentially limited to PEM? When I use your invocation in CI (albeit with a 'normal' OpenSSH private key; not a PEM), I'm greeted with Scripting does not use standalone parameters. The parameters you have specified on command-line will not be used. Your command-line syntax is probably wrong. \\ Opening session using command-line parameter in scripting is deprecated. Use 'open' command instead. \\ Searching for host... \\ Host "C" does not exist. and I'm then greeted with a winscp> prompt. Jun 3 at 13:04
  • @SeanAllred 1) Make sure you have the latest version of WinSCP. 2) If the key path contains spaces, it needs to be wrapped to double quotes. Jun 4 at 17:46
  • This was in CI (private instance at gitlab.megacorp.net; sorry!). I was downloading straight from sourceforge's /latest/ (looks like the zip was called WinSCP-6.1-Portable – sha1-sums: pastebin.com/Ti16zCSX) and my key paths did not have spaces. I recognize this might be the end of the road investigation-wise :-/ thanks anyway; and thanks for all the time and effort you put into WinSCP :-) Jun 6 at 7:10
26

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/

3
  • 2
    The question is about "Windows command line". Dec 19, 2017 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, 2017 at 15:51
  • This anwer may not be as useful as just referring to the official documentation for this program at the.earth.li/~sgtatham/putty/latest/htmldoc/… although admittedly this answer is shorter. But at the time of this writing the first link is to putty.org which is not positively referred to by the.earth.li/~sgtatham/putty/latest/htmldoc/… so I feel I should downvote this as more likely to cause more harm than help (unless I later notice this answer is fixed in a change newer than 2022-July-10 about 7:45am PST)
    – TOOGAM
    Jul 10, 2022 at 14:44
10

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.

4
  • 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, 2017 at 8:59
  • 2
    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, 2019 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, 2021 at 8:48
  • @KalibZen The author only states that he is working in a client environment on windows. The WSL2 can do the job with this answer. Sep 16, 2022 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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