title: Bouncy Castle C# - getting public key object from byte array
body: <p>I'm trying to get BouncyCastle's AsymmetricKeyParameter object from a byte array that contains a public key. The particular example I'm facing is a RSA public key encoded in the simplest way possible, namely as a DER sequence with two INTEGER objects, modulus and public exponent; however, I cannot assume it's always going to be RSA (could also be ECDSA).</p>
  <p>Examples I've found on the web suggest using PublicKeyFactory.CreateKey, I do it like this:</p>
  <pre><code>Asn1InputStream ais = new Asn1InputStream(content);
  Asn1Object publicKey = ais.ReadObject();
  var pubkeyParams = PublicKeyFactory.CreateKey(publicKey.GetEncoded());
  </code></pre>
  <p>Unfortunately, this gives me an exception &quot;Unknown object in GetInstance: Org.BouncyCastle.Asn1.DerInteger\nParameter name: obj&quot;</p>
  <p>I guess that has something to do with how the key is encoded in that byte array. Unfortunately that's not something I have control of; but it's not like it's completely botched either, since OpenSSL command line utility doesn't protest when I run <code>openssl rsa -in publicKey.bin -RSAPublicKey_in -inform DER -text</code> on the bin file with key data.</p>
  <p>I'd appreciate any help in how to proceed. I'm thinking about trying to parse key data as RSA, if that doesn't work then as ECDSA, then fold when exception happens. I'm not sure how to do it with BC though...</p>