git key_load_public:格式无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42863913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:53:30  来源:igfitidea点击:

key_load_public: invalid format

gitgithubsshpublic-key

提问by JordanGS

I used PuTTY Key Generator to generate a 4096 bit RSA-2 key with a passphrase.

我使用 PuTTY Key Generator 生成带有密码的 4096 位 RSA-2 密钥。

I save the .ppk and an openSSL format public key. The putty format public key doesn't work.

我保存了 .ppk 和一个 openSSL 格式的公钥。腻子格式公钥不起作用。

In any case, my error is as follows:

无论如何,我的错误如下:

$ ssh -T [email protected]
key_load_public: invalid format
Enter passphrase for key '/c/Users/Dan/.ssh/id_rsa':
Hi Dan! You've successfully authenticated, but GitHub does not provide shell access.

What's the issue?

有什么问题?

I use Pageant to load the keys and i use Git Bash to try the ssh connection. I've also loaded the key into GitHub, not sure what i'm doing wrong.

我使用 Pageant 加载密钥,并使用 Git Bash 尝试 ssh 连接。我还将密钥加载到 GitHub 中,不确定我做错了什么。

I've tried adding a new line and not adding a new line in GitHub

我试过在 GitHub 中添加新行而不是添加新行

回答by samthecodingman

As Rolandmentioned in their answer, it's a warning that the ssh-agentdoesn't understand the format of the public key and even then, the public key will not be used locally.

正如罗兰在他们的回答中提到的,这是一个警告,即ssh-agent不理解公钥的格式,即使如此,公钥也不会在本地使用。

However, I can also elaborate and answer why the warning is there. It simply boils down to the fact that the PuTTY Key Generatorgenerates two different public key formatsdepending on what you do in the program.

但是,我也可以详细说明并回答为什么会出现警告。归结为这样一个事实,即PuTTY 密钥生成器会根据您在程序中执行的操作生成两种不同的公钥格式

Note:Throughout my explanation, the key files I will be using/generating will be named id_rsawith their appropriate extensions. Furthermore, for copy-paste convenience, the parent folder of the keys will be assumed to be ~/.ssh/. Adjust these details to suit your needs as desired.

注意:在我的整个解释中,我将使用/生成的关键文件将id_rsa以其适当的扩展名命名。此外,为了方便复制粘贴,密钥的父文件夹将被假定为~/.ssh/. 根据需要调整这些细节以满足您的需要。

The Formats

格式

Link to the relevant PuTTY documentation

相关 PuTTY 文档的链接

SSH-2

SSH-2

When you savea key using the PuTTY Key Generatorusing the "Save public key" button, it will be saved in the format defined by RFC 4716.

当您保存使用的关键PuTTY密钥生成使用“保存公钥”按钮,它将被保存在由定义的格式RFC 4716

Example:

例子:

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "github-example-key"
AAAAB3NzaC1yc2EAAAABJQAAAQEAhl/CNy9wI1GVdiHAJQV0CkHnMEqW7+Si9WYF
i2fSBrsGcmqeb5EwgnhmTcPgtM5ptGBjUZR84nxjZ8SPmnLDiDyHDPIsmwLBHxcp
pY0fhRSGtWL5fT8DGm9EfXaO1QN8c31VU/IkD8niWA6NmHNE1qEqpph3DznVzIm3
oMrongEjGw7sDP48ZTZp2saYVAKEEuGC1YYcQ1g20yESzo7aP70ZeHmQqI9nTyEA
ip3mL20+qHNsHfW8hJAchaUN8CwNQABJaOozYijiIUgdbtSTMRDYPi7fjhgB3bA9
tBjh7cOyuU/c4M4D6o2mAVYdLAWMBkSoLG8Oel6TCcfpO/nElw==
---- END SSH2 PUBLIC KEY ----

OpenSSH

开放式SSH

Contrary to popular belief, this format doesn't get saved by the generator. However it is generated and shown in the text box titled "Public key for pasting into OpenSSH authorized_keys file". To save it as a file, you have to manually copy it from the text box and paste it into a new text file.

与流行的看法相反,这种格式不会被生成器保存。但是,它是在标题为“用于粘贴到 OpenSSH 授权密钥文件的公共密钥”的文本框中生成并显示的。要将其另存为文件,您必须手动从文本框中复制它并将其粘贴到新的文本文件中。

For the key shown above, this would be:

对于上面显示的密钥,这将是:

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAhl/CNy9wI1GVdiHAJQV0CkHnMEqW7+Si9WYFi2fSBrsGcmqeb5EwgnhmTcPgtM5ptGBjUZR84nxjZ8SPmnLDiDyHDPIsmwLBHxcppY0fhRSGtWL5fT8DGm9EfXaO1QN8c31VU/IkD8niWA6NmHNE1qEqpph3DznVzIm3oMrongEjGw7sDP48ZTZp2saYVAKEEuGC1YYcQ1g20yESzo7aP70ZeHmQqI9nTyEAip3mL20+qHNsHfW8hJAchaUN8CwNQABJaOozYijiIUgdbtSTMRDYPi7fjhgB3bA9tBjh7cOyuU/c4M4D6o2mAVYdLAWMBkSoLG8Oel6TCcfpO/nElw== github-example-key

The format of the key is simply ssh-rsa <signature> <comment>and can be created by rearranging the SSH-2 formatted file.

密钥的格式很简单ssh-rsa <signature> <comment>,可以通过重新排列 SSH-2 格式的文件来创建。

Regenerating Public Keys

重新生成公钥

If you are making use of ssh-agent, you will likely also have access to ssh-keygen.

如果您正在使用ssh-agent,您可能还可以访问ssh-keygen.

If you have your OpenSSH Private Key (id_rsafile), you can generate the OpenSSH Public Key File using:

如果您有 OpenSSH 私钥(id_rsa文件),则可以使用以下方法生成 OpenSSH 公钥文件:

ssh-keygen -f ~/.ssh/id_rsa -y > ~/.ssh/id_rsa.pub

If you only have the PUTTY Private Key (id_rsa.ppkfile), you will need to convert it first.

如果您只有 PUTTY 私钥(id_rsa.ppk文件),则需要先将其转换。

  1. Open the PuTTY Key Generator
  2. On the menu bar, click "File" > "Load private key"
  3. Select your id_rsa.ppkfile
  4. On the menu bar, click "Conversions" > "Export OpenSSH key"
  5. Save the file as id_rsa(without an extension)
  1. 打开PuTTY 密钥生成器
  2. 在菜单栏上,单击“文件”>“加载私钥”
  3. 选择您的id_rsa.ppk文件
  4. 在菜单栏上,单击“转换”>“导出 OpenSSH 密钥”
  5. 将文件另存为id_rsa(不带扩展名)

Now that you have an OpenSSH Private Key, you can use the ssh-keygentool as above to perform manipulations on the key.

现在您有了一个 OpenSSH 私钥,您可以使用上述ssh-keygen工具对密钥执行操作。

Bonus: The PKCS#1 PEM-encoded Public Key Format

奖励:PKCS#1 PEM 编码的公钥格式

To be honest, I don't know what this key is used for as I haven't needed it. But I have it in my notes I've collated over the years and I'll include it here for wholesome goodness. The file will look like this:

老实说,我不知道这个密钥是做什么用的,因为我不需要它。但我把它放在我多年来整理的笔记中,为了有益健康,我会把它包括在这里。该文件将如下所示:

-----BEGIN RSA PUBLIC KEY-----
MIIBCAKCAQEAhl/CNy9wI1GVdiHAJQV0CkHnMEqW7+Si9WYFi2fSBrsGcmqeb5Ew
gnhmTcPgtM5ptGBjUZR84nxjZ8SPmnLDiDyHDPIsmwLBHxcppY0fhRSGtWL5fT8D
Gm9EfXaO1QN8c31VU/IkD8niWA6NmHNE1qEqpph3DznVzIm3oMrongEjGw7sDP48
ZTZp2saYVAKEEuGC1YYcQ1g20yESzo7aP70ZeHmQqI9nTyEAip3mL20+qHNsHfW8
hJAchaUN8CwNQABJaOozYijiIUgdbtSTMRDYPi7fjhgB3bA9tBjh7cOyuU/c4M4D
6o2mAVYdLAWMBkSoLG8Oel6TCcfpO/nElwIBJQ==
-----END RSA PUBLIC KEY-----

This file can be generated using an OpenSSH Private Key (as generated in "Regenerating Public Keys" above) using:

可以使用 OpenSSH 私钥(如上面的“重新生成公钥”中生成的)使用以下方法生成此文件:

ssh-keygen -f ~/.ssh/id_rsa -y -e -m pem > ~/.ssh/id_rsa.pem

Alternatively, you can use an OpenSSH Public Key using:

或者,您可以通过以下方式使用 OpenSSH 公钥:

ssh-keygen -f ~/.ssh/id_rsa.pub -e -m pem > ~/.ssh/id_rsa.pem

References:

参考:

回答by PradeepNama

There is a simple solution if you can install and use puttygen tool. Below are the steps. You should have the passphrase of the private key.

如果您可以安装和使用 puttygen 工具,则有一个简单的解决方案。下面是步骤。您应该拥有私钥的密码。

step 1: Download latest puttygenand open puttygen

第一步:下载最新的puttygen并打开puttygen

step 2: Load your existing private key file, see below image

第 2 步:加载您现有的私钥文件,见下图

Load an existing private key

加载现有的私钥

step 3: Enter passphrase for key if asked and hit ok

第 3 步:如果询问,请输入密钥的密码并点击确定

enter paasphrase

输入短语

step 4: as shown in the below image select "conversion" menu tab and select "Export OpenSSH key"

第 4 步:如下图所示,选择“转换”菜单选项卡并选择“导出 OpenSSH 密钥”

save OpenSSH file

保存 OpenSSH 文件

Save new private key file at preferred location and use accordingly.

在首选位置保存新的私钥文件并相应地使用。

回答by Roland Smith

It seems that sshcannot read your public key. But that doesn't matter.

似乎ssh无法读取您的公钥。但这没关系。

You upload your public keyto github, but you authenticate using your private key. See e.g. the FILES section in ssh(1).

您将公钥上传到 github,但您使用私钥进行身份验证。参见例如 中的 FILES 部分ssh(1)

回答by Hojin Choi

In the case you copy your public key with clipboard and paste it, it may happen the public key string can be broken which contains new-line.

如果您使用剪贴板复制您的公钥并粘贴它,则可能会发生包含换行符的公钥字符串可能会被破坏。

Make sure your public key string formed as one line.

确保您的公钥字符串形成为一行。

回答by Jignesh Rawal

Instead of directly saving the private key Go to Conversions and Export SSh Key. Had the same issue and this worked for me

而不是直接保存私钥转到转换和导出 SSh 密钥。有同样的问题,这对我有用

回答by arberg

I had the same warning. It was a very old key. I regenerated a key on the current OpenSSH 7, and the error was gone.

我有同样的警告。这是一把非常古老的钥匙。我在当前的 OpenSSH 7 上重新生成了一个密钥,错误消失了。