title: Generate a CMS PKCS#7 file with BouncyCastle in C#
body: <p>I'm starting to cryptography.
  I need to generate a PKCS # 7 file with XML, an RSA private key (which is not included in the certificate is a file extension .key) and a certificate .cer extension.</p>
  
  <p>For this i'm using a BouncyCastle.</p>
  
  <p>Edit:</p>
  
  <p>Thanks @khlr for your help but I can not solve my problem. When sending the data to the AC returns me "Invalid CMS". 
  I have this code:    </p>
  
  <pre><code>public static byte[] FirmaBytesMensaje(byte[] argBytesMsg, X509Certificate2 argCertFirmante)
  {
      try
      {
          //Add message in object ContentInfo 
          ContentInfo infoContenido = new ContentInfo(argBytesMsg);
          SignedCms cmsFirmado = new SignedCms(infoContenido);
  
  
          CmsSigner cmsFirmante = new CmsSigner(argCertFirmante);
          cmsFirmante.IncludeOption = X509IncludeOption.EndCertOnly;
  
  
          // Sign message PKCS #7 
          cmsFirmado.ComputeSignature(cmsFirmante);
  
  
          // Encodeo el mensaje PKCS #7. 
          return cmsFirmado.Encode();
      }
      catch (Exception excepcionAlFirmar)
      {
          throw new Exception("***Error: " + excepcionAlFirmar.Message);
      }
  }
  </code></pre>
  
  <p>Signing on PKCS # 7 but this makes with a "PFX" certificate, that is, that contains the private key in a ".pfx" file. 
  When I use the OpenSSL command:</p>
  
  <pre><code>openssl smime -sign -signer cert.crt -inkey private.key -out file.xml.cms -in file.xml -outform PEM -nodetach
  </code></pre>
  
  <p>The AC responds well.
  How I can do this with BouncyCastle and cer and key files? I am going crazy! :-(</p>