title: Read DER private key in C# using BouncyCastle
body: <p>I am trying to read an RSA private key into .Net using BouncyCastle to test data I have previously encrypted. The encrypted data is working fine using the public key and Bouncy Castle and I have also used the same private key as below (which is DER format) to successfully decrypt my data in a PHP application but I don't know why I can't create the private key in .Net to do the same thing:</p>
  
  <pre><code>byte[] privatekey = File.ReadAllBytes(@"C:\Users\Luke\privkey.der");
  var rsaKeyParameters = (RsaKeyParameters)PrivateKeyFactory.CreateKey(privatekey);
  </code></pre>
  
  <p>The second line throws an exception:</p>
  
  <blockquote>
    <p>"unknown object in factory: DerInteger\r\nParameter name: obj"</p>
  </blockquote>
  
  <p>I also tried using a stream instead of a byte array and the same error occurs. The key pair was created using OpenSSL and as mentioned, decryption works in PHP using openssl_private_decrypt() and the same key as in the .Net code. I also tried a PEM format of the same key and that also didn't work (but I don't think BC supports PEM directly anyway)</p>
  
  <p>Has anyone done this before? Thanks</p>