![PuTTY 密钥生成器在加载 PEM 文件时显示“无法加载私钥(不是私钥)”](https://linux22.com/image/1448063/PuTTY%20%E5%AF%86%E9%92%A5%E7%94%9F%E6%88%90%E5%99%A8%E5%9C%A8%E5%8A%A0%E8%BD%BD%20PEM%20%E6%96%87%E4%BB%B6%E6%97%B6%E6%98%BE%E7%A4%BA%E2%80%9C%E6%97%A0%E6%B3%95%E5%8A%A0%E8%BD%BD%E7%A7%81%E9%92%A5%EF%BC%88%E4%B8%8D%E6%98%AF%E7%A7%81%E9%92%A5%EF%BC%89%E2%80%9D.png)
我有一个 PKCS12 格式的证书(“gridcert.p12”),我正在尝试将其转换为可以在 PuTTY 中使用的密钥对。
我正在使用 OpenSSL 将 PKCS12 文件转换为一对 PEM 文件,一个用于私钥,另一个用于公钥。但是,PuTTY 不接受 PEM 密钥。因此,我尝试通过 PuTTY 密钥生成器(“puttygen.exe”)将私钥 PEM 转换为 PPK 格式。但是,当我尝试加载密钥时,PuTTY 密钥生成器给出了标题中的错误消息。
我的工作文档粘贴如下。任何想法都非常感谢。
# 12:45 22.04.2015 #
goal: convert "gridcert.p12" to a PuTTY-useable keypair.
first, secluded prior work to subdirectory "old attempts".
i decided to keep "gridcert.p12" as-is, rather than regenerate it.
current OpenSSL version is "OpenSSL 1.0.2 22 Jan 2015" by The OpenSSL Project.
no snapshot; just the stable release.
it's meant for a 32-bit system, i think (it's stored in directory "OpenSSL-Win32").
current download of "putty.exe" and "puttygen.exe" from Simon Tatham (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html).
downloaded versions for Windows on Intel x86.
"puttygen.exe": beta 0.64.0.0
"putty.exe" : beta 0.63.10188.0
using Windows 8.1 on 64-bit Acer laptop.
created mixed public/private key file with:
13:05:21,98>C:\OpenSSL-Win32\bin\openssl pkcs12 -in gridcert.p12 -out keys.pem -nodes
Enter Import Password:
MAC verified OK
created individual private and public keys, respectively, with:
13:10:40,36>C:\OpenSSL-Win32\bin\openssl pkcs12 -in gridcert.p12 -out privatekey.pem -nodes -nocerts
Enter Import Password:
MAC verified OK
13:11:25,53>C:\OpenSSL-Win32\bin\openssl pkcs12 -in gridcert.p12 -out publickey.pem -nodes -nokeys
Enter Import Password:
MAC verified OK
both keys start with "MII" and end with "==".
the private key starts with "MIIEvwIBADANBgk...".
the public key starts with "MIIIxzCCBq+gAwI...".
the mixed-key file corroborates these observations.
both files seem to end with a new-line in both Notepad and Notepad++.
the private key has 1653 characters in its body, counting line breaks (not at the beginning or end).
the public key has 3050 characters in its body, counting line breaks (not at the beginning or end).
opening "puttygen.exe", pushing "Load", and selecting any of "privatekey.pem", "publickey.pem", and "key.pem" generates the error box "Couldn't load private key (not a private key)".
running "puttygen.exe" as administrator and trying to Load "privatekey.pem" generates the same error.
tried recreating public/private keys with no "-nodes" specifier, instead using "-clcerts":
14:51:12,59>C:\OpenSSL-Win32\bin\openssl pkcs12 -in gridcert.p12 -out publickey_clcerts.pem -clcerts -nokeys
Enter Import Password:
MAC verified OK
15:06:56,17>C:\OpenSSL-Win32\bin\openssl pkcs12 -in gridcert.p12 -out privatekey_clcerts.pem -clcerts -nocerts
Enter Import Password:
MAC verified OK
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
"puttygen.exe" still gives the same error when i try to Load "privatekey_clcerts.pem" and "publickey_clcerts.pem".
the "_clcerts.pem" files look very similar to "privatekey.pem" and "publickey.pem".
their bodies both start with "MII" and end with "=".
i think that the new public key file is the same as the old.
the new private key file is definitely somewhat different from the old.
in "puttygen.exe", tried clicking "Conversions -> Import" and selecting "privatekey.pem", but the same error message was returned.
tried generating a PuTTY key to see if it runs into same problems.
opened "puttygen.exe" and clicked "Generate".
moved mouse in blank space to generate randomness (that is so cool).
entered a passphrase.
clicked "Save private key" and saved it as "privateputty.ppk".
clicked "Save public key" and saved it as "publicputty", no extension.
clicked "Load" and selected "privateputty.ppk".
clicked "Conversions -> Export OpenSSH key", saved it as "privateputtySSH.pem".
clicked "Load" and selected "privateputtySSH.pem".
a box prompted me for the passphrase, which i entered.
a box appeared telling me that i'd successfully imported an OpenSSH SSH-2 private key.
i clicked "Okay" and the PuTTY Key Generator stood before me.
the PuTTY-generated private key is quite different from the OpenSSL-generated key.
it begins right with "-----BEGIN RSA PRIVATE KEY-----
"; i.e., no header information.
additionally, "privatekey.pem" indicated the key body with "-----BEGIN PRIVATE KEY-----".
instead of launching straight into the body after it's "----" intro, "privateputtySSH.pem" has some header info before its gobbledygook body.
it almost certainly doesn't start the body with "MII", but it's a bit difficult to tell where the body starts.
it terminates the body with "==", like before.
finally, it closes with "-----END RSA PRIVATE KEY-----" and a newline.
答案1
BEGIN PRIVATE KEY
标记 OpenSSL 最近开始使用的 PKCS#8 私钥格式,而 PuTTY 仅期望“传统”/“PEM”BEGIN RSA PRIVATE KEY
格式。
将密钥输入其中openssl rsa
以将其转换为旧格式。
两个密钥都以 开头,MII…
因为在 Base64 编码中,ASN.1 序列就是以 开头的,但 PKCS#8另外里面还有钥匙类型,而不是依赖 PEM“BEGIN…”标头。您可以使用 来查看差异openssl asn1parse
。