How to generate SSH 2 RSA key in C# application?

2019-04-10 02:53发布

I would like to write an application that will generate SSH 2 RSA public and private keys as well.

I would like to get the keys as format as the PuTTY Key Generator can generate.

enter image description here

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?

Thank you very much!

标签: c# ssh rsa putty
1条回答
闹够了就滚
2楼-- · 2019-04-10 03:14

I found a solution: Download the Chilkat.NET Nuget from the Nuget Gallery. With the help of this library, you can generate an RSA Key Pair in the following way:

public ISshKeyPair RsaKeyPair()
{
   Chilkat.SshKey key = new Chilkat.SshKey();

   bool success;
   int numBits;
   int exponent;
   numBits = 2048;
   exponent = 65537;
   success = key.GenerateRsaKey(numBits, exponent);
   var sshKeyPair = new SshKeyPair();
   sshKeyPair.PublicKey = key.ToOpenSshPublicKey();
   sshKeyPair.PrivateKey = key.ToPuttyPrivateKey(false);
   if (!success) RsaKeyPair();
   return sshKeyPair;
}

Cheers!

查看更多
登录 后发表回答