Cannot convert ssh.com key to ppk format using Puttygen

5

I am having issues converting a private key from ssh.com format to putty format. Upon importing of the private key, I am met with the following error.

Couldn't load private key (key does not begin with magic number).

I am unable to figure out what is wrong with the key. I have generated another sample key with the same problem below that is not used for anything obviously and without passphrase. I would appreciate any help from anyone. Thanks in advance.

---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----
Comment: "[2048-bit rsa, Fri May 16 2014]"
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDe7GR0INPoKwedwq8XjY
CRO/2Zwl9ssRj1vqiF4BtUdP9Dgz79gZIoaOHGKxtTj0cesBkdcrvbU5rGbqidOfn7RPjo
AeX8L817bRbk276PYh823pEnclPlTUBJDzfP5IK1u7Gbp35YlZnEcXFbaMF8vcBQ6tr+/i
R8Z2akeNmDYTW8U9Xi+MPYeXxwSoI9cQrPztqzlkCschhgM1GAthkEjBRGQi4T9DuzwGgF
R0hx+n5TqIHcoMyBZAP/Lq9wEZsmWO/rboqxDNhgaR3ID3zL7mqp7vN9hZ/q19wG4QAZbH
yEExMHkZmwTipk0gUQCPf5AJYAbPrGLuGWqICG+349AgMBAAECggEAIk0tEtpnSPLwD9Qg
VYcyOIbK2bE5NjQU82FUNI9cnTUIfzavh1n756hlzzScVpH2H1B4+NlJ8zBs4SovW0wLvW
BqTWbXYOPpIhwR2QLAEneGdo6uNqvmoWKu6N8E10Ih471fEaSm8hII8FuFVgurNBrNgBX3
PyPTcOoHNvkafRa7Uv89HgoENo1lQRD01bA6Rj7nTIV4L3gffZgHOoxMVmFprLY8GPbPrO
Y9/ecbbFdM5Xd5bN20swJ9ekXJIrhtQl7mgRdvk0tVVbpkb6uAxhPiw4k/+imrNJXk8goK
LJ9Km8DVfIacsqxGg/oQdtQT90Xi/2CE3wYWStUwwqCCoQKBgQD5yqZWe1lwPBj4Bp0Lxb
xXncazi+7tYHNrBaooYD5HEW5t1s5iU6nsd1UtXlOAccjvSAK1Ng7dr7JQz+zdmWFYHsfF
Qh+RwVPoaj4dnJ3EJt7wpcW4jt1B6FFxnXW5A58tRk78qFGTjIJH/DFfVUMsKkESljLjN9
JUkS+c9xVTXQKBgQDkdsnLQF89QbHt2UNm7XhybGV4oJE3FOysTqxzrXY+JAqNUahc2miX
dEU6lcNrM/6g0LrQxgumvhDcMg/bFDqvwDI+qNPiMOJyXLjd6Nhj+UF9xn9w7kXvliLwek
Nb4o37/iF6EsJbZFKAkg4/Qt93eovLgT0wbVQyeJrlybwIYQKBgHHcFqAbD7SiIDuGQDWq
h2RkrbsP0PPw4AtrhJls7VZVssFshZy6SB1ac7//XA6TTyUK7PkO0A3q1VwSuwRzsrSJK0
RWFK/KsOWi7/dteVh842hMF+k9bctqCx2/yXZxGTB4+O4gBzc7d/M6GIM9CSZriEfhoQaD
XmNf3pUs51KdAoGAOn/EKfwgpuHEep+wJ5HUzo+858dNh6oe8/vtIm2fIkg+P3NSRfxS3b
0S4O5qLHAS2gC4ZS7g2uA+FvPmpXUnlcsD0pGON5PG1/uwBnpRtj+jql4CUdthIvjxl1vn
FYcMTiMDVWuqQpaMnYlIhqBL8h753rEIBvdnoMjK+N3P/OECgYA2jMJaFtoKVjA6oUXj56
uDLe09I17VV5mq46HjjMUPlU9HuGF3d5U5a54NMq3beFLiVHdhC1M4s8UWv/vuXgmPrAmo
knD4thxCbu7iuBx5oI9C5zPvBT4hmH3LMVp/iEFO7nmBcSe3PjnaYCn8/LAs38F5oxaTLN
mHTVR7pcw89w==
---- END SSH2 ENCRYPTED PRIVATE KEY ----

wakaka

Posted 2014-05-16T05:29:53.447

Reputation: 171

Could you also post a [small] key with a passphrase? Also, what program generates those keys? – user1686 – 2014-05-16T14:18:01.787

1What do you mean by small key? The program that generated the key is an old F-Secure SSH File transfer client v5.4 – wakaka – 2014-05-16T15:10:59.507

Answers

2

I finally figured out what is wrong. Apparently, the keys are in the format called F-Secure FSCLM Private Key Format. A keygen application was included in the application folder that can be used to convert the key into the correct format that can be read by Puttygen.

wakaka

Posted 2014-05-16T05:29:53.447

Reputation: 171

3

The private key doesn't appear to be in ssh.com format – its headers are a lie.

The first few letters, MII…, decode to bytes 30 82, which is the start of an DER format ASN.1 "sequence" – and SSH.com does not use ASN.1 in its key format, but OpenSSL/OpenSSH does, for unencrypted RSA keys.

(However, if the key had a passphrase, it wouldn't be recognizable this way. See also note at the end.)

In comparison, SSH.com keys are most likely to start with P2/56…, or bytes 3f 6f f9 eb …. This kind of thing is what the words "magic numbers" mean: many formats are recognizable by specific bytes at the beginning – sometimes defined in the format spec, sometimes just a side effect of the data structures.

So if you ignore the text headers and try to Base64-decode the main blob of data, you'll find an ASN.1 structure exactly like OpenSSL uses for its old "bare RSA" format private keys. This is the same format that OpenSSH uses for most private key types.

# cat key.txt | sed "/^---- /d; /^Comment:/d" | base64 -d > key.raw

# dumpasn1 key.raw

   0 1212: SEQUENCE {
   4    1:   INTEGER 0
   7   13:   SEQUENCE {
   9    9:     OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
  20    0:     NULL
         :     }
  22 1190:   OCTET STRING, encapsulates {
  26 1186:     SEQUENCE {
  30    1:       INTEGER 0
  33  257:       INTEGER
         :         00 DE EC 64 74 20 D3 E8 2B 07 9D C2 AF 17 8D 80
         :         91 3B FD 99 C2 5F 6C B1 18 F5 BE A8 85 E0 1B 54
         :         74 FF 43 83 3E FD 81 92 28 68 E1 C6 2B 1B 53 8F
         :         47 1E B0 19 1D 72 BB DB 53 9A C6 6E A8 9D 39 F9
                   [etc.]
 294    3:       INTEGER 65537
[etc.]

# openssl rsa -in key.raw -inform DER -noout -text

Private-Key: (2048 bit)
modulus:
    00:de:ec:64:74:20:d3:e8:2b:07:9d:c2:af:17:8d:
    80:91:3b:fd:99:c2:5f:6c:b1:18:f5:be:a8:85:e0:
    [etc.]
publicExponent: 65537 (0x10001)
privateExponent:
    22:4d:2d:12:da:67:48:f2:f0:0f:d4:20:55:87:32:
    38:86:ca:d9:b1:39:36:34:14:f3:61:54:34:8f:5c:
    [etc.]
[etc.]

As you can see, OpenSSL recognizes it as its own format, containing unencrypted RSA parameters.

You can "recover" this key by:

  1. removing both ---- lines, and the Comment: line

  2. adding -----BEGIN RSA PRIVATE KEY----- at the top

  3. adding -----END RSA PRIVATE KEY----- at the bottom

  4. telling PuTTYgen to import it as an OpenSSH private key

However, with encrypted keys, the headers that OpenSSL expects are somewhat different – in particular, the DEK-Info: header contains the encryption algorithm and the salt (or is it the IV?) that was used for encrypting the data. Without it, you cannot decrypt the key even if you know the passphrase.

So the above steps will work only if the key's passphrase is removed beforehand.

user1686

Posted 2014-05-16T05:29:53.447

Reputation: 283 655

Be careful in step 2 noted above not to have any trailing spaces after -----BEGIN RSA PRIVATE KEY-----. Puttygen will not load if there are extra spaces. – Cale Sweeney – 2019-01-30T01:19:22.853

Wow thanks so much for all that information with me being the newbie at all these key formats. I will give it a try back at work on Monday and report with the result or question if I have any then. – wakaka – 2014-05-16T15:09:26.990

1Ask you said, I tried changing the top and the bottom header to RSA but the error PuttyGen shows now is "Couldn't load private key (ASN.1 decoding failure) – wakaka – 2014-05-19T00:55:21.633