19

In my Mac book pro with OS Majave, I used SSH-KEYGEN generate a new SSH key for node.js module SSH2. However, I got error message: Cannot parse privateKey: Unsupported key format

I validated the key with ssh command, it just works fine. But not with the node.js module SSH2.

In the private key file, the header is -----BEGIN OPENSSH PRIVATE KEY----- while the end is -----END OPENSSH PRIVATE KEY-----.

I checked module ssh2-streams keyParser.js. The regexp pattern RE_HEADER_OPENSSH_PRIV does not include my header. Can anyone help?

mihai
  • 37,072
  • 9
  • 60
  • 86
George Zhang
  • 371
  • 1
  • 2
  • 9
  • I found this issue in https://github.com/mscdex/ssh2-streams/issues/100 . Anyone have any idea to fall back the ssh-keygen to old version? – George Zhang Nov 20 '18 at 20:00
  • what parameters did you use for `ssh-keygen` ? – mihai Nov 21 '18 at 08:16
  • I tried "-o " and almost all combinations already. But it still use OPENSSH in the header. For now, I just copied the old private key for the SSH2. – George Zhang Nov 22 '18 at 18:47
  • the thing is, you shouldn't be using -o. See this article https://tutorialinux.com/convert-ssh2-openssh/ – mihai Nov 22 '18 at 19:26
  • For updates w.r.t to `node-ssh` module refer this: https://stackoverflow.com/questions/30366606/connecting-ssh-server-with-private-key-in-node-js/76405892#76405892 – Prateek Jain Jun 05 '23 at 11:53

2 Answers2

23

Had the same problem, found the solution here.

basically use the PEM option when you create your key:

ssh-keygen -m PEM -t rsa 
Dozon Higgs
  • 473
  • 2
  • 5
12

You could also convert your existing key, rather than create a new one (make sure you back it up before you run the following command as it will overwrite your original one):

ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

(Original answer from https://serverfault.com/a/950686)

Arnaud Valle
  • 1,673
  • 1
  • 16
  • 25