如何使用 Python 生成 SSH 密钥对
我正在尝试编写一个脚本来为我生成 SSH 身份密钥对。
from M2Crypto import RSA
key = RSA.gen_key(1024, 65337)
key.save_key("/tmp/my.key", cipher=None)
文件 /tmp/my.key
现在看起来很棒。
通过运行 ssh-keygen -y -f /tmp/my.key > /tmp/my.key.pub 我可以提取公钥。
我的问题是如何从 python 中提取公钥?使用 key.save_pub_key("/tmp/my.key.pub")
保存类似的内容:
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADASDASDASDASDBarYRsmMazM1hd7a+u3QeMP
...
FZQ7Ic+BmmeWHvvVP4Yjyu1t6vAut7mKkaDeKbT3yiGVUgAEUaWMXqECAwEAAQ==
-----END PUBLIC KEY-----
当我正在寻找类似的内容时:
ssh-rsa AAAABCASDDBM$%3WEAv/3%$F ..... OSDFKJSL43$%^DFg==
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用
密码学
!pycrypto
不再处于积极开发中,如果可能的话,您应该使用密码学。从 6 月开始,还可以生成 SSH 公钥:注意:您至少需要
1.4.0 版本
。注意:如果您的 SSH 客户端不理解此私钥格式,请替换
PKCS8
与TraditionalOpenSSL
。Use
cryptography
!pycrypto
is not in active development anymore and if possible you should be using cryptography. Since June it's possible to generate SSH public keys as well:Note: You need at least version
1.4.0
.Note: If your SSH client does not understand this private key format, replace
PKCS8
withTraditionalOpenSSL
.以防万一未来有任何旅行者想要这样做。 RSA 模块现在支持以 OpenSSH 格式写出公钥(可能在之前的帖子中还没有)。所以我认为你可以做你需要做的事情:
文件用“wb”打开,因为密钥必须以二进制模式写入。
显然不要将您的私钥存储在 /tmp 中...
Just in case there are any future travellers looking to do this. The RSA module support writing out the public key in OpenSSH format now (possibly didn't at the time of earlier posts). So I think you can do what you need with:
The files are opened with a 'wb' as the keys must be written in binary mode.
Obviously don't store you're private key in /tmp...
编辑 05/09/2012:
我刚刚意识到 pycrypto 已经有这个:
这段代码对我有用:
Edit 05/09/2012:
I just realized that pycrypto already has this:
This code works for me:
ssh 使用的密钥只是 base64 编码,我不太了解 M2Crypto,但快速概述后,似乎您可以通过这种方式执行您想要的操作:
我没有使用 SSH 测试生成的密钥,所以请让我知道它是否有效(我应该认为)
The key used by ssh is just base64 encoded, i don't know M2Crypto very much, but after a quick overview it seems you could do what you want this way:
I didn't test the generated key with SSH, so please let me know if it works (it should i think)
ssh-keygen 的 Base64 解码版本输出到 key.pub() 的内容,密钥文件的格式为
The base64 decoded version of ssh-keygen output to the contents of key.pub() the format of the keyfile is
如果需要,您也可以使用 ssh-keygen 本身。
您可以扩展它来创建您的文件,然后只需使用
open
来读取内容,但我在这里重点关注从现有密钥创建 .pub 密钥。If you want, you could just also use
ssh-keygen
itself.You can extend this to also create your file, and just use
open
to read the content later, but i focused on creating a .pub key from an already existing key here.只是猜测......但你尝试过这样的事情吗?:
Just guessing... but have you tried something like this?:
当它是一个对象时,你能从中获取 AAAA...Dfg== 字符串吗?如果是这样,您可以简单地自己打开一个文件并保存它,而不是使用内置的 save_pub_key 函数。
Can you get the AAAA...Dfg== string out of it while it's an object? If so, you could simply open a file yourself and save that instead of using the built in save_pub_key function.
下面是一个使用 Twisted Conch 库的示例,该库在幕后利用了 PyCrypto。您可以在 http://twistedmatrix.com/ 找到 API 文档文档/current/api/twisted.conch.ssh.keys.html:
Here is an example using the Twisted Conch library which leverages PyCrypto under the covers. You can find the API documentation at http://twistedmatrix.com/documents/current/api/twisted.conch.ssh.keys.html:
您可以按照 文档:
You can use
pycryptodome
as described in documentation:pip install ssh-key-maker
import ssh_key_maker
#对于 Windows 用户
ssh_key_maker.generate_ssh_key()
pip install ssh-key-maker
import ssh_key_maker
#for windows users
ssh_key_maker.generate_ssh_key()