如何在C#应用程序中生成SSH 2 RSA密钥? [英] How to generate SSH 2 RSA key in C# application?
问题描述
我想编写一个也会生成SSH 2 RSA公钥和私钥的应用程序.
I would like to write an application that will generate SSH 2 RSA public and private keys as well.
我想以PuTTY密钥生成器可以生成的格式获取密钥.
I would like to get the keys as format as the PuTTY Key Generator can generate.
借助ChilKat,我还可以生成公钥和私钥,但是我不知道如何获得这种格式.
With the help of ChilKat I can generate the public and private keys as well, but I don't know how to get that kind of format.
是否有任何样本以这种格式获取密钥?或者我错过了一些东西?
Is there any sample to get the keys at that format or I missed something?
非常感谢!
推荐答案
SSH密钥格式非常复杂;如果您想自己实现它,请此,此答案可能是一个好的开始.
The SSH key format is rather complex; if you want to implement it yourself, this, this and this answer might be a good start.
但是,实际上其他人已经完成了这项工作,并创建了一个用于生成SSH密钥的NuGet包: SshKeyGenerator.现在,该软件包同时适用于.NET Framework和.NET Standard.源代码可在 GitHub 上获得.
However, someone else actually already did the work and created a NuGet package for generating SSH keys: SshKeyGenerator. Right now the package is offered for both .NET Framework and .NET Standard. The source code is available on GitHub.
示例代码:
static void Main(string[] args)
{
int keyBits = 2048;
string keyComment = "mykey";
var keygen = new SshKeyGenerator.SshKeyGenerator(keyBits);
Console.WriteLine(keygen.ToPrivateKey());
Console.WriteLine(keygen.ToRfcPublicKey(keyComment));
}
生成(为缩短可读性)
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA7ejxsqTKLMLht6HVC57l59mMzQNIVdeMaPzxM14VYjkyU1rg
...
L4NAI1BCgGpSC+iY3QuVwK8GVphVSzNrOQj97Uuhx0WYsEdPzJvjQw==
-----END RSA PRIVATE KEY-----
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD...nFX7Rhou4OdkDGA1 mykey
请注意,该软件包当前不支持设置私钥密码.
Note that the package currently does not support setting a private key passphrase.
这篇关于如何在C#应用程序中生成SSH 2 RSA密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!