title: How to import a signed SSL certificate using Bouncy Castle in C# (Mono/Xamarin)?
body: <p>I used Bouncy Castle to generate a private key, as well as a PKCS10 CSR, which I then send to a remote server to be signed. I get back a standard base64 encoded signed SSL certificate in response as a string. The question is, how do I import the signed certificate from a string, and then save both the private key and signed certificate as a PKCS12 (.PFX) file?</p>
  
  <p>Additionally, how do I bundle in a CA certificate to be included within the PFX file?</p>
  
  <pre><code>// Generate the private/public keypair
  RsaKeyPairGenerator kpgen = new RsaKeyPairGenerator ();
  CryptoApiRandomGenerator randomGenerator = new CryptoApiRandomGenerator ();
  kpgen.Init (new KeyGenerationParameters (new SecureRandom (randomGenerator), 2048));
  AsymmetricCipherKeyPair keyPair = kpgen.GenerateKeyPair ();
  
  // Generate the CSR
  X509Name subjectName = new X509Name ("CN=domain.com/name=Name");
  Pkcs10CertificationRequest kpGen = new Pkcs10CertificationRequest ("SHA256withRSA", subjectName, keyPair.Public, null, keyPair.Private);
  string certCsr = Convert.ToBase64String (kpGen.GetDerEncoded ());
  
  // ** certCsr is now sent to be signed  **
  // ** let's assume that we get "certSigned" in response, and also have the CA **
  string certSigned = "[standard signed certificate goes here]";
  string certCA = "[standard CA certificate goes here]";
  
  // Now how do I import certSigned and certCA
  // Finally how do I export everything as a PFX file?
  </code></pre>