title: How to load PuTTYgen keys in Bouncy castle
body: <p>I'm playing around with encrypting. I have got stucked on loading the keys with BouncyCastle. The keys have been generated by PuTTYgen (SSH-2 RSA 4096 bit). Is there maybe some other way to load it?</p>
  <pre class="lang-none prettyprint-override"><code>PuTTY-User-Key-File-2: ssh-rsa
  Encryption: aes256-cbc
  Comment: rsa-key-20160508
  Public-Lines: 12
  </code></pre>
  <p>I get:</p>
  <blockquote>
  <p>An unhandled exception of type 'System.IO.IOException' occurred in BouncyCastle.Crypto.dll<br />
  Additional information: unexpected end-of-contents marker*&quot;</p>
  </blockquote>
  <pre><code>const string pubkey = @&quot;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=&quot;;
  
  const string prikey = @&quot;...&quot;;
  
  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);
  </code></pre>
  <p><strong>ADDED:</strong>
  I generated a new key with  &quot;ssh-keygen -t rsa -b 4096&quot; and tryed to load it with pemreader:</p>
  <pre><code>   const string pubkey = @&quot;AAAAB3NzaC1yc2EAAAADAQABAAACAQDGzR88DIxKRUvEJDAa0zcRq0O39M5i9vwkb9r+mPi56eYPEZJfMcZZmrtMoBoCJM3g0CmdHHktanRnjwXI+Gw6zRIbe3Lnz8rq+IyCDQnJQzHMlFJkwt7tuFzLWwNoWL5bWN0+Y6h8pB6KudPxsutQfNVTAyI00JhhhMk1T0FSF82BHP+HKI+lsGrTaBhDHnbZryk5Bf00sAJLVvS5kE7LOthVAWHGKZbV/PJnDcXQN8Hxto9mWy0iELTsUYzwf1VfIvB1Ugyv/rs3Vn5A98p2BaXidwYrgc3NhKliESQyl9KYggF3ody99zlMzWQfdmpfJEBzjxjNIMaD/NFBgTLHSbEzyXDVLcvCocmNaJeEQ4uGcqqw2Oe7d9RDZikllttLCtPQSQ49N/1JI5CRHEn9bz7RNX4aZWPbzw1l5y6fcsTXcPsVHb+bGmhcobSbWZvAnLMrHA0FI+47yNrBlp+qXBXfxO3/sSK3yU1lJEUosmfb3nPvvt0bt0TlwLYC12gMLk1Z0ZAoHxR7qw7la/HoZXhQqqi57RdDWbHiX6zxFPDVMd48fc8m9Wnwv2+AIcBRsErnL4YWT/4Pb57+ehg+HbKy5cCgo6wsHYB9IjoXIfNQAz2dAbXHr2C4zo0BKlabZSqv+ZoTBP5NwSZItMeRv1eCreg7ejJ5HR8NIJaAZQ==&quot;;
      
     var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(new StringReader(pubkey));
     // KeyParameter is still null
     var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject();
  </code></pre>
  <p><strong>ADDED:</strong></p>
  <p>I was able to generate a public key and also load a unencrypted private key
  (<a href="https://rietta.com/blog/2012/01/27/openssl-generating-rsa-key-from-command/" rel="nofollow noreferrer">https://rietta.com/blog/2012/01/27/openssl-generating-rsa-key-from-command/</a>)</p>
  <blockquote>
  <p>openssl genrsa -des3 -out private.pem 4096</p>
  <p>openssl rsa -in private.pem -outform PEM -pubout -out public.pem</p>
  <p>openssl rsa -in private.pem -out private_unencrypted.pem -outform PEM</p>
  </blockquote>
  <pre><code>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);
  </code></pre>
  <p>But I was not able to load the encrypted private key...</p>