title: Sign Xml with digital certificate in format of PKCS#7 in DER (ITU-T Rec. X.690)
body: <p>I have a <code>.xml</code> file that has to be signed with <code>digital certificate</code> in format of <code>PKCS#7 version 1.5 (RFC 2315)</code> and <code>DER (ITU-T Recommendation X.690</code></p>
  
  <p>That <code>.xml</code> will be send to a govt. <code>WebService</code> that only accept the format I mentioned upwards.</p>
  
  <p>What I'm able to do - thanks to <a href="http://rooseboom.net/?p=23" rel="nofollow">this website</a> is digitaly sign <code>.xml</code> with the <code>.pfx</code> file that I generated with <code>Certificate Export Wizard</code> explained below. The class that I'm using to sign is down on mentioned website or <a href="https://www.dropbox.com/sh/5phooctpyc6b0gw/b8hIFORKFJ/XmlDsig.cs" rel="nofollow">here</a>.</p>
  
  <p>From what I tried to understand so far I will need to sign the <code>.xml</code> with <code>.pfx</code> file according to <code>X.690</code> standards but I'm only able to access this namespace:</p>
  
  <pre><code>using System.Security.Cryptography.X509Certificates;
  </code></pre>
  
  <p>which is obviously for X.509 format.</p>
  
  <p>Note: </p>
  
  <p>There are several things I'm confused about - to export the certificate into <code>.pfx</code> I'm using <code>Internet Explorer - Certificate Export Wizard</code> from there I'm able to:</p>
  
  <p>Yes - export private keys - then it will be generated in <code>PKCS#12</code> but <code>.pfx</code></p>
  
  <p>No - do not export private keys - Certificate according to standards <code>Cryptographic Message Syntax Standard - PKCS#7</code> that I guess I need but I would receive <code>.p7b</code> file</p>
  
  <p>I must say that I am a newbie in certificates and digital signatures so I'm not even sure if I'm correctly exporting the certificat and the second thing is how I can sign according to <code>X.690</code> standards. </p>
  
  <p>May I know how to sign according to X.690 format please?</p>
  
  <p>Thank you everyone for your time and replies.</p>
  
  <p>My code is following:</p>
  
  <pre><code>bool res = false;
  try
  {
      byte[] certBytes = System.IO.File.ReadAllBytes(pvkPath);
      X509Certificate2 cert = new X509Certificate2(certBytes, certPass);
      XmlDocument doc = new XmlDocument();
  
      doc.Load(xmlPath);
  
      // sign the document with the private key of the certificate
      XmlDsig.SignXml(doc, cert);
  
      // save the document
      doc.Save(xmlSavePath);
  
      // verify that the document has a signature
      bool hasSignature = XmlDsig.HasSignatureElement(doc);
      return res = true;
  }
  catch (Exception)
  { return res; }
  </code></pre>