如何在Bouncy城​​堡中加载PuTTYgen键

时间:2016-05-08 19:41:32

标签: c# bouncycastle putty

我正在玩加密。我一直坚持用BouncyCastle加载密钥。密钥由PuTTYgen(SSH-2 RSA 4096位)生成。是否有其他方法可以加载它?

const string pubkey = @"AAAAB3NzaC1yc2EAAAABJQAAAgEAlX+AsmDF7FoR7D2jWYJTKLD6ZH6FAY9v2vPa
5+ICSQCydphiUbiJwsb3sl4yJIB6uOw4fM+22F0U/P1pxdGqUabqixNOC5X9xBzI
KbMt2xcYof/JCaNskHE2teguxdCeLlQ30QIxTRD76y6TqISS+anTtpMiGFs/LNOX
yDp7H3S5OuvRjoTRLoocEwIFNbpnkq8D+RVSdV1kkqVSMwzwRgBRRCkBfZAYW9VY
qlWNwbLCiGw3qi5mu2qorxqqF0tO2l0gLFNYdbnf9+bKavqiV7wMn3mVbcFtRhe/
29klqxos6V8Of71Q7IShFz9IXq5KdRdJoGyNJrNXKoF87UCPppa4lmOlfY40E/c5
w/LMjf63c/xx8ji7v6hY0sHt7XdWUB3V1XBPScypk7PxNS13/RtdzjeOn/8W9tcV
tqX6b7XFU/AwIrkBDa47JGnuOJnmzmMzHDPKeZwaEQT4BZRzUa6yKq3EZCLymkBB
W9lRQkKY3qg8OJX+FhAQ7c5pCOl3PFmFjuDMbRWTaF/nJj/qPfvND+w7a86fxUUC
HPokcYGLVZUBWTfnx3q/StbjaQBQDPMxogLh/yCZywHFqO0xGaA8SQAM2Vyd7r7l
waPQhFIJhVrXuaz0Ok+TE5jqe5yTB0gZ8pcVDq/GWRXBAUwXu50UeZBurcGHEXk5
HhFdFlk=";

const string prikey = @"...";

var publickey = Convert.FromBase64String(pubkey);
//var privatekey = Convert.FromBase64String(prikey);

Org.BouncyCastle.Crypto.AsymmetricKeyParameter publicAsymmetricKey = Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(publickey);
//Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateAsymmetricKey = Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey(privatekey);

我明白了:

  

未处理的类型' System.IO.IOException'发生在BouncyCastle.Crypto.dll中   附加信息:意外的内容结束标记*"

   const string pubkey = @"AAAAB3NzaC1yc2EAAAADAQABAAACAQDGzR88DIxKRUvEJDAa0zcRq0O39M5i9vwkb9r+mPi56eYPEZJfMcZZmrtMoBoCJM3g0CmdHHktanRnjwXI+Gw6zRIbe3Lnz8rq+IyCDQnJQzHMlFJkwt7tuFzLWwNoWL5bWN0+Y6h8pB6KudPxsutQfNVTAyI00JhhhMk1T0FSF82BHP+HKI+lsGrTaBhDHnbZryk5Bf00sAJLVvS5kE7LOthVAWHGKZbV/PJnDcXQN8Hxto9mWy0iELTsUYzwf1VfIvB1Ugyv/rs3Vn5A98p2BaXidwYrgc3NhKliESQyl9KYggF3ody99zlMzWQfdmpfJEBzjxjNIMaD/NFBgTLHSbEzyXDVLcvCocmNaJeEQ4uGcqqw2Oe7d9RDZikllttLCtPQSQ49N/1JI5CRHEn9bz7RNX4aZWPbzw1l5y6fcsTXcPsVHb+bGmhcobSbWZvAnLMrHA0FI+47yNrBlp+qXBXfxO3/sSK3yU1lJEUosmfb3nPvvt0bt0TlwLYC12gMLk1Z0ZAoHxR7qw7la/HoZXhQqqi57RdDWbHiX6zxFPDVMd48fc8m9Wnwv2+AIcBRsErnL4YWT/4Pb57+ehg+HbKy5cCgo6wsHYB9IjoXIfNQAz2dAbXHr2C4zo0BKlabZSqv+ZoTBP5NwSZItMeRv1eCreg7ejJ5HR8NIJaAZQ==";

   var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(new StringReader(pubkey));
   // KeyParameter is still null
   var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject();

增加: 我使用" ssh-keygen -t rsa -b 4096"生成了一个新密钥。并尝试使用pemreader加载它:

var publickey = Convert.FromBase64String(pubkey);
var privatekey = Convert.FromBase64String(prikey);

var asnprivate = Org.BouncyCastle.Asn1.Asn1Object.FromStream(new MemoryStream(privatekey));
var privStruct = new Org.BouncyCastle.Asn1.Pkcs.RsaPrivateKeyStructure((Org.BouncyCastle.Asn1.Asn1Sequence)asnprivate);

AsymmetricKeyParameter publicAsymmetricKey = PublicKeyFactory.CreateKey(publickey);

增加:

我能够生成公钥并加载未加密的私钥 (https://rietta.com/blog/2012/01/27/openssl-generating-rsa-key-from-command/

  

openssl genrsa -des3 -out private.pem 4096

     

openssl rsa -in private.pem -outform PEM -pubout -out public.pem

     

openssl rsa -in private.pem -out private_unencrypted.pem -outform PEM

for

但是我无法加载加密的私钥......

2 个答案:

答案 0 :(得分:1)

PuTTY .ppk密钥格式是PuTTY专有的。

我很确定在Bouncy城​​堡中没有对.ppk文件的支持。

您需要将密钥转换为OpenSSH格式。为此,请参阅How to convert SSH keypairs generated using PuttyGen(Windows) into key-pairs used by ssh-agent and KeyChain(Linux)

答案 1 :(得分:1)

使用openssl PEM,我能够使它工作。我继续了解如何加载受保护的私钥。

感谢您的所有输入。

using System;
using System.IO;
using System.Text;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1;

namespace EncryptTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            const string pubkey = @"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAuQhcZKNnIC2bvJhI5Yzq
oH9uGAHuiAt9bOXdIrsaeEwsIUMuyNfXkLozwlcJb0YLgqhpP3gNLb58WCb1AvkV
ZHgzLQbay93HwmBiEPW6LZIKGsHwh1awBmJpeWhU8h3oNP+KpNq44H+jAeM9G9En
W0y7+AUbe3pnisOWVZHmM72A2mLq1piCOjmvjegzgB8/rU7ZkRrNadeFO7DBP0Ew
6eElS3iXfFHJmbK+L5lL0pFxvUMRfGVC33NQkuSoWiQJZsYyUgIk6WSOQm3t5fw6
VESMTfd1xKlaBGp7IrWksthEF+XAu5ziAcSd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g7ccmXyGB/SIHRlAoIBAQCqF/P8D+T8uEiiQron39NAGgSM
FFyi1uj3IeDrDPgdsLT5TtpFOnHpUMjhxVEhdIQm2K4MH44/aBW40CgS+OGHclay
SzwdtfHyPuK1H8TwaJqgA5NYu/xTyiR8QqRE+qwwh5wMH4f4ErhKglnQJVNxHeo7
ZlRul6vFgS3uaaEyaPXrG5pFRPeLlqllJxQ5Z1mQtqaK9U0dSCykxW6zaz3d8YXE
BIe/0Vc1/0d80lkJk/3SnLGIAiC2C9dpmA6RmgkGa0cyD5QPmXmYXHFT+tA7S/lV
W+5C6ZO0aaOmO+F+W82ZJ92FRd8CITasoPO9zxalJBEGD/s5ehnRhhizLqjY";

            var publickey = Convert.FromBase64String(pubkey);
            var privatekey = Convert.FromBase64String(prikey);

            var asnprivate = Asn1Object.FromStream(new MemoryStream(privatekey));
            var privStruct = new RsaPrivateKeyStructure((Asn1Sequence)asnprivate);

            RsaKeyParameters privateAsymmetricKey = new RsaKeyParameters(true, privStruct.Modulus, privStruct.PrivateExponent);
            RsaKeyParameters publicAsymmetricKey = (RsaKeyParameters)PublicKeyFactory.CreateKey(publickey);

            var inputBytes = Encoding.UTF8.GetBytes("the message");

            Console.WriteLine("--- Message: ----");
            Console.WriteLine(Encoding.UTF8.GetString(inputBytes));

            IAsymmetricBlockCipher cipher = new RsaEngine();
            cipher.Init(true, publicAsymmetricKey);
            var cipheredBytes = cipher.ProcessBlock(inputBytes, 0, inputBytes.Length);

            Console.WriteLine("--- Enc utf8: ----");
            Console.WriteLine(Encoding.UTF8.GetString(cipheredBytes));

            Console.WriteLine("--- Enc Base64: ----");
            Console.WriteLine(Convert.ToBase64String(cipheredBytes));

            cipher.Init(false, privateAsymmetricKey);
            var deciphered = cipher.ProcessBlock(cipheredBytes, 0, cipheredBytes.Length);

            Console.WriteLine("--- Dec: ----");
            Console.WriteLine(Encoding.UTF8.GetString(deciphered));
            Console.ReadLine();
        }
    }
}