title: Signature XML verification failed C#
body: <p>I need to implements EBICS protocol and sign the HPB XML request. </p>
  
  <p>My signature is correctly generated in my XML file but when the server verify it, i get the following return and I don't understand why : </p>
  
  <blockquote>
    <p>Authentication Signature Invalid</p>
  </blockquote>
  
  <p>Can you help me to solve my problem ? </p>
  
  <p>This is my code to generate my signature : </p>
  
  <p><strong>Method : SignXml()</strong></p>
  
  <pre><code>public static void SignXml()
      {
          CryptoConfig.AddAlgorithm(typeof(RsaPkCs1Sha256SignatureDescription), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
          XmlDocument xmlDoc = new XmlDocument();
          xmlDoc.PreserveWhitespace = false;
          xmlDoc.Load("hpbtest.xml");
          RSACryptoServiceProvider Key = new RSACryptoServiceProvider();
          X509Certificate2 Cert = new X509Certificate2("cert.pfx", "password", X509KeyStorageFlags.Exportable);
          Key.FromXmlString(Cert.PrivateKey.ToXmlString(true));
          // Create a SignedXml object.
          PrefixedSignedXML signedXml = new PrefixedSignedXML(xmlDoc);
          // Add the key to the SignedXml document.
          signedXml.SigningKey = Key;
          signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
          // Create a reference to be signed.
          Reference reference = new Reference();
          reference.Uri = "#xpointer(//*[@authenticate='true'])";
          reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";
          // Add an enveloped transformation to the reference.
          XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform();
          env.Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
          reference.AddTransform(env);
          // Add the reference to the SignedXml object.
          signedXml.AddReference(reference);
          // Compute the signature.
          signedXml.ComputeSignature("ds");
          // Get the XML representation of the signature and save
          // it to an XmlElement object.
          XmlElement xmlDigitalSignature = signedXml.GetXml("ds");
          // Append the element to the XML document.
          xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
          xmlDoc.Save("hpbtest.xml");
      }
  </code></pre>
  
  <p><strong>Class PrefixedSignedXml</strong></p>
  
  <pre><code>public class PrefixedSignedXML : SignedXml
  {
      XmlDocument xmlDocumentToSign;
      public PrefixedSignedXML(XmlDocument document)
          : base(document)
      {
          xmlDocumentToSign = document;
      }
  
      public PrefixedSignedXML(XmlElement element)
          : base(element)
      { }
  
      public PrefixedSignedXML()
          : base()
      { }
  
      public void ComputeSignature(string prefix)
      {
          this.BuildDigestedReferences();
          AsymmetricAlgorithm signingKey = this.SigningKey;
          if (signingKey == null)
          {
              throw new CryptographicException("Cryptography_Xml_LoadKeyFailed");
          }
          SignatureDescription description = CryptoConfig.CreateFromName(this.SignedInfo.SignatureMethod)as SignatureDescription;
          if (description == null)
          {
              throw new CryptographicException("Cryptography_Xml_SignatureDescriptionNotCreated");
          }
          HashAlgorithm hash = description.CreateDigest();
          if (hash == null)
          {
              throw new CryptographicException("Cryptography_Xml_CreateHashAlgorithmFailed");
          }
          this.GetC14NDigest(hash, prefix);
          this.m_signature.SignatureValue = description.CreateFormatter(signingKey).CreateSignature(hash);
      }
  
      public XmlElement GetXml(string prefix)
      {
          XmlElement e = this.GetXml();
          SetPrefix(prefix, e);
          return e;
      }
  
      private void BuildDigestedReferences()
      {
          Type t = typeof(SignedXml);
          MethodInfo m = t.GetMethod("BuildDigestedReferences", BindingFlags.NonPublic | BindingFlags.Instance);
          m.Invoke(this, new object[] { });
      }
  
      private byte[] GetC14NDigest(HashAlgorithm hash, string prefix)
      {
          XmlDocument document = new XmlDocument();
          document.PreserveWhitespace = false;//Aucune influence sur la signature
          XmlElement e = this.SignedInfo.GetXml();
          document.AppendChild(document.ImportNode(e, true));
          Transform canonicalizationMethodObject = this.SignedInfo.CanonicalizationMethodObject;
          SetPrefix(prefix, document.DocumentElement); 
          canonicalizationMethodObject.LoadInput(document);
          return canonicalizationMethodObject.GetDigestedOutput(hash);
      }
  
      private void SetPrefix(String prefix, XmlNode node)
      {
          foreach (XmlNode n in node.ChildNodes)
              SetPrefix(prefix, n);
          node.Prefix = prefix;
      }
  
      public override XmlElement GetIdElement(XmlDocument document, string idValue)
      {
          XmlElement matchingElement = null;
          try
          {
              matchingElement = base.GetIdElement(document, idValue);
          }
          catch (Exception idElementException)
          {
              Trace.TraceError(idElementException.ToString());
          }
  
          if (matchingElement == null)
          {
              // at this point, idValue = xpointer(//*[@authenticate='true'])
              string customXPath = idValue.TrimEnd(')');
              customXPath = customXPath.Substring(customXPath.IndexOf('(') + 1);
              matchingElement = xmlDocumentToSign.SelectSingleNode(customXPath) as XmlElement;
          }
          return matchingElement;
      }
  }
  </code></pre>
  
  <p><strong>Class RsaPkCs1Sha256SignatureDescription</strong></p>
  
  <pre><code>public class RsaPkCs1Sha256SignatureDescription : SignatureDescription
  {
      public static void Register()
      {
          CryptoConfig.AddAlgorithm(typeof(RsaPkCs1Sha256SignatureDescription),"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
      }
      public RsaPkCs1Sha256SignatureDescription()
      {
          KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";
          DigestAlgorithm = "System.Security.Cryptography.SHA256Managed";
          FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";
          DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
      }
  
      public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
      {
          var asymmetricSignatureDeformatter = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm);
          asymmetricSignatureDeformatter.SetKey(key);
          asymmetricSignatureDeformatter.SetHashAlgorithm("SHA256");
          return asymmetricSignatureDeformatter;
      }
  
      public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
      {
          var asymmetricSignatureFormatter =
              (AsymmetricSignatureFormatter)CryptoConfig.CreateFromName(FormatterAlgorithm);
          asymmetricSignatureFormatter.SetKey(key);
          asymmetricSignatureFormatter.SetHashAlgorithm("SHA256");
          return asymmetricSignatureFormatter;
      }
  }
  </code></pre>
  
  <p><strong>Final signature result</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ebicsNoPubKeyDigestsRequest xmlns="http://www.ebics.org/H003" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ebics.org/H003 http://www.ebics.org/H003/ebics_keymgmt_request.xsd" Version="H003" Revision="1"&gt;
    &lt;header authenticate="true"&gt;
      &lt;static&gt;
        &lt;HostID&gt;EBIXQUAL&lt;/HostID&gt;
        &lt;Nonce&gt;234AB2340FD2C23035764578FF3092D3&lt;/Nonce&gt;
        &lt;Timestamp&gt;2015-12-04T10:07:27.123Z&lt;/Timestamp&gt;
        &lt;PartnerID&gt;AD598&lt;/PartnerID&gt;
        &lt;UserID&gt;EF056&lt;/UserID&gt;
        &lt;OrderDetails&gt;
          &lt;OrderType&gt;HPB&lt;/OrderType&gt;
          &lt;OrderAttribute&gt;DZHNN&lt;/OrderAttribute&gt;
        &lt;/OrderDetails&gt;
        &lt;SecurityMedium&gt;0000&lt;/SecurityMedium&gt;
      &lt;/static&gt;
      &lt;mutable /&gt;
    &lt;/header&gt;
    &lt;AuthSignature&gt;
      &lt;ds:SignedInfo&gt;
        &lt;ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /&gt;
        &lt;ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /&gt;
        &lt;ds:Reference URI="#xpointer(//*[@authenticate='true'])"&gt;
          &lt;ds:Transforms&gt;
            &lt;ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /&gt;
          &lt;/ds:Transforms&gt;
          &lt;ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /&gt;
          &lt;ds:DigestValue&gt;2glaCqpZf0XMkqB+twtAsJtKo9MWg60FaMBwJ91xNGQ=&lt;/ds:DigestValue&gt;
        &lt;/ds:Reference&gt;
      &lt;/ds:SignedInfo&gt;
      &lt;ds:SignatureValue&gt;FISRuQ0WTanCkvhc9/m/9OIHCd+hfPl4WjqVlhPQx5AoBe/yNFBQoXwC+d3os7kaxDvZghIosLzcIc/69M5YUAn/njlWAzYL+pqtXXy3Ea9SxMqeb3BwPVLMfkgY2TUo7QIlnv+omUPRq0PzDi50iZFv4sh+TBdWIEs29JJET/48MP+d67MqpdN5NRtM3C2QDWpRhe4erKoRCJC1OW+DkIHdKZ4lqJpKp1cdcooCRGXod9+6usxbfD8LU0Ow7wTJl2crUya9Elx+gxGUogo7bBrRnRpWM/5ih12KcoXQ/mKRpwKCbk+vYRF9E66/2SVjRZSkmFm1xeENRiZJ5tJDRg==&lt;/ds:SignatureValue&gt;
    &lt;/AuthSignature&gt;
    &lt;body /&gt;
  &lt;/ebicsNoPubKeyDigestsRequest&gt;
  </code></pre>
  
  <p>I'm really try everything and I don't understand why it not work :(</p>
  
  <p>Thank you in advance ! </p>
  
  <p>Thomas</p>