title: Constructing RSACryptoServiceProvider from X509Certificate2 without LDAP request being made
body: <p>When the line to extract the public key is executed, an LDAP request is sent:</p>
  
  <pre><code>        this.certificate = new X509Certificate2(buffer);
          System.Security.Cryptography.X509Certificates.PublicKey key = this.certificate.PublicKey;
  </code></pre>
  
  <blockquote>
    <p>50    0.853745000 xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx LDAP    404 searchRequest(1) "" baseObject </p>
  </blockquote>
  
  <p>...which I believe is authenticating the currently logged on user.  I really need to avoid this call, as on a customer system this causes a long delay because of network configuration.</p>
  
  <p>I'm assuming it's attempting to do some authentication around some kind of key store, but as in this case the certificate is all contained in the buffer provided, all I want is for the key to be used without this request being sent.</p>
  
  <p>All I actually want is to create an RSACryptoServiceProvider from the private key in the certificate.  I tried a few methods I've found on here involving GetPrivateKey, but struggled to get anything to work.</p>
  
  <p>Thanks in advance!</p>
  
  <p><em>EDIT</em> Test program:</p>
  
  <pre><code>    static void Main( string[] args )
      {
          var certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(@"E:\Temp\Cert.cer");
          System.Security.Cryptography.X509Certificates.PublicKey key = certificate.PublicKey;
      }
  </code></pre>
  
  <p>The certificate I tested with can be found here: <a href="https://dl.dropboxusercontent.com/u/106407239/Cert.cer" rel="nofollow">Cert.cer</a></p>
  
  <p>Yes, it's not the strongest signature or key, before I get comments!</p>
  
  <p>Thanks again.</p>
  
  <p>EDIT: I actually worked around this by using a suggestion to use BouncyCastle.  I use this to parse the certificate:</p>
  
  <pre><code>X509CertificateParser parser = new X509CertificateParser();
  Org.BouncyCastle.X509.X509Certificate cert = parser.ReadCertificate(buffer);
  </code></pre>
  
  <p>I then extract the modulus and exponent and push them into a Microsoft RSAParameters:</p>
  
  <pre><code>RsaKeyParameters key = (RsaKeyParameters)cert.GetPublicKey();
  
  // Construct a microsoft RSA crypto service provider using the public key in the certificate
  RSAParameters param = new RSAParameters();
  param.Exponent = key.Exponent.ToByteArrayUnsigned();
  param.Modulus = key.Modulus.ToByteArrayUnsigned();
  </code></pre>
  
  <p>I can then construct the Microsoft RSACryptoServiceProvider from this:</p>
  
  <pre><code>using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider())
  {
      provider.ImportParameters(param);
      byte[] rsaBlock = provider.Encrypt(preMasterSecret, false);
      this.Client.Writer.Write(rsaBlock);
  }
  </code></pre>