title: XMLDSIG XML Digital Signature. What bytes are really signed?
body: <p><strong>Good day to all, first of all WALL OF TEXT WARNING! I've done a lot of research and i'll post some code I'm sure will help someone. I've put italic in the Blah blah parts, leaving the "important" parts in normal text.</strong></p>
  
  <p><strong>Short question:</strong> Are the SignedInfo node bytes (from [60] to [62]) the one that really get signed (that means "sha1ed", then RSAed, then B64es and the incrusted into the SignatureValue node?"</p>
  
  <p><strong>Long Question:</strong>
  <em>The problem is simply do a standar XMLDSIG on a XML file created by myself (that means it will always look similar). The software should be able to run on android devices and windows phone 6.1 devices (especifically the CN50 intermec portable computer). The last part is relevant because that means i cant use the simplistic way of XML signing that the .NET framework provides (I'm looking at you, System.Security.Cryptography.Xml library). So, unable to use the mentioned library i tried to sign the document "by hand".
  The most obvious proof and noob oriented info i could find is this page: <a href="http://www.di-mgt.com.au/xmldsig2.html" rel="nofollow">http://www.di-mgt.com.au/xmldsig2.html</a> where they do precisely that: a completely step by step guide on how to sign a XML document with and enveloped signature.
  The canonicalization part doesn't worry me since, like I say, the document will be created by be, and such i will take precautions to not introduce elements that could potentially ruin the hashing calculation part. That said, i tried to do the canonicalizacion in Java usinf the XOM library, however the results obtained where.. strange.</em></p>
  
  <p><strong>Before I go on, a data filled, unsigned document was correctly signed using the XMLSEC library (available here <a href="http://www.aleksey.com/xmlsec/" rel="nofollow">http://www.aleksey.com/xmlsec/</a>) using the following command:</strong></p>
  
  <pre><code>xmlsec --sign --privkey-pem PEMFILE.pem,PEMFILE.pem --node-xpath "(//*[local-name()='Signature' and namespace-uri()='http://www.w3.org/2000/09/xmldsig#'])[last()]" --id-attr:Id "http://www.w3.org/2000/09/xmldsig#:Signature" --id-attr:Id "http://www.w3.org/2000/09/xmldsig#:SignatureValue" UnsignedDocument.xml &gt; SignedDocument.xml
  </code></pre>
  
  <p><em>Also, i did the signing using the .NET framework on my computer using the standar .NET 4.0 framework with the following (Note1: its not a real production code, but as an example it could help someone. Note2: Here i used the PFX certificate installed on my computer)</em></p>
  
  <pre><code>X509Store Certificados = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  Certificados.Open(OpenFlags.ReadOnly);
  
  foreach (X509Certificate2 Resultado in Certificados.Certificates)
     if (Resultado.Thumbprint == "16182C4CB0440D86DBD567B6A9C1963C02E41B9A")
       return Resultado;
  
  throw new Exception("No hay un certificado instalado para el RFC que se indicó.");
  </code></pre>
  
  <p><em>And the signing is done with the following</em></p>
  
  <pre><code>public static void SignXmlFile2(string FileName, string SignedFileName, X509Certificate2 Key)
          {
              // Create a new XML document.
              XmlDocument doc = new XmlDocument();
  
              // Format the document to ignore white spaces.
              doc.PreserveWhitespace = true;
  
              // Load the passed XML file using it's name.
              doc.Load(new XmlTextReader(FileName));
              XmlNode root = doc.DocumentElement;
  
              // Create a SignedXml object.    
              SignedXml signedXml = new SignedXml(doc);
  
              // Obtenemos el objeto de llave privada del certificado
              RSACryptoServiceProvider Llave = Key.PrivateKey as RSACryptoServiceProvider;
              Llave.ExportParameters(false);
              // Add the key to the SignedXml document. 
              signedXml.SigningKey = Llave;
  
              // Specify a canonicalization method.
              signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigC14NWithCommentsTransformUrl;
  
              // Set the InclusiveNamespacesPrefixList property.
              XmlDsigC14NWithCommentsTransform canMethod = (XmlDsigC14NWithCommentsTransform)signedXml.SignedInfo.CanonicalizationMethodObject;
  
          // Create a reference to be signed.
              Reference reference = new Reference();
              reference.Uri = "";
  
          // Add an enveloped transformation to the reference.
              XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
              reference.AddTransform(env);
  
          // Add the reference to the SignedXml object.
              signedXml.AddReference(reference);
  
              keyInfo.AddClause(new RSAKeyValue((RSA)Llave));
              signedXml.KeyInfo = keyInfo;*/
  
          /* Creamos el nodo &lt;KeyInfo&gt; con el subnodo &lt;X509Data&gt;, poniendo dentro de éste el 
          certificado, su número de serie y la entidad emisora del mismo (primero estos dos últimos
          como subnodo &lt;X509IssuerSerial&gt;), y agregando todo al objeto firmante. */
              KeyInfoX509Data NodoX509Data = new KeyInfoX509Data();
              NodoX509Data.AddCertificate(Key);
              signedXml.KeyInfo = new KeyInfo();
              signedXml.KeyInfo.AddClause(NodoX509Data);
  
          // Compute the signature.
              signedXml.GetHashCode();
              signedXml.ComputeSignature();
  
          // Get the XML representation of the signature and save 
          // it to an XmlElement object.
              XmlElement xmlDigitalSignature = signedXml.GetXml();
  
              // Append the element to the XML document.
              doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
  
              // Save the signed XML document to a file specified 
              // using the passed string.
              XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
              doc.WriteTo(xmltw);
              xmltw.Close();
          }
  </code></pre>
  
  <p><em>With the above i can get two equivalent documents and both signed correctly. 
  So, I have a unsigned and a correctly signed document (obtained from two sources - C# and XMLSEC), from there i know the correct SHA1 Hash value of the document (called DigestValue) and the correct signature (called SignatureValue). I have something to compare to.
  Like i said, i tried to do the canonicalization in Java, but could get the hash correctly so, instead of banging my head to the head hoping to find some sort of illumination as to how i'm messing this up, i simply decide to assume the input document <strong>will be canonnicalized</strong>. And if someone says "thats impossible because the line breaks will change with canonicalization" i simply would reply <strong>my input file wont have any line breaks</strong>. Once again, the document will be created by me.
  Following that approach, i can get the correct digest value of the unsigned document with the following code:</em></p>
  
  <p>JAVA:</p>
  
  <pre><code>/**
   * Generates SHA-1 digest of the provided data.
   *
   * @param data the data to digest
   * @return SHA-1 digest of the provided data.
   */
  public static byte[] sha1Digest(byte[] data) {
    MessageDigest mdSha1 = null;
    try {
      mdSha1 = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e1) {
      Log.e(LOG_TAG, "Error initializing SHA1 message digest");
    /*} catch (NoSuchProviderException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();*/
  }
    mdSha1.update(data);
    byte[] sha1hash = mdSha1.digest();
    return sha1hash;
  }
  </code></pre>
  
  <p><strong>That means the input document is canonically equivalent to the one being signed with the methods stated above.</strong></p>
  
  <p>The Sha1 Bytes are</p>
  
  <blockquote>
    <p>[55, -59, -1, 71, -62, 26, 57, 126, 76, 7, 120, 53, -38, -51, 8, 38,
    127, -29, 5, 25]</p>
  </blockquote>
  
  <p><strong>And in B64</strong></p>
  
  <blockquote>
    <p>N8X/R8IaOX5MB3g12s0IJn/jBRk=</p>
  </blockquote>
  
  <p><strong>HERE COMES THE TROUBLING PART</strong></p>
  
  <p><strong>According to the theory (from differents sources) i need to insert the digestValue inside the  node</strong>. <em>My approach was to read the bytes of an empty, canonicalized,  node like this</em></p>
  
  <pre><code>&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;&lt;SignedInfo&gt;&lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"&gt;&lt;/CanonicalizationMethod&gt;&lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"&gt;&lt;/SignatureMethod&gt;&lt;Reference URI=""&gt;&lt;Transforms&gt;&lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"&gt;&lt;/Transform&gt;&lt;/Transforms&gt;&lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"&gt;&lt;/DigestMethod&gt;&lt;DigestValue&gt;&lt;/DigestValue&gt;&lt;/Reference&gt;&lt;/SignedInfo&gt;&lt;SignatureValue&gt;&lt;/SignatureValue&gt;&lt;KeyInfo&gt;&lt;X509Data&gt;&lt;X509Certificate&gt;&lt;/X509Certificate&gt;&lt;/X509Data&gt;&lt;/KeyInfo&gt;&lt;/Signature&gt;
  </code></pre>
  
  <p><strong>The above is translated into bytes:</strong></p>
  
  <blockquote>
    <p>[60, 83, 105, 103, 110, 97, 116, 117, 114, 101, 32, 120, 109, 108,
    110, 115, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46,
    119, 51, 46, 111, 114, 103, 47, 50, 48, 48, 48, 47, 48, 57, 47, 120,
    109, 108, 100, 115, 105, 103, 35, 34, 62, 60, 83, 105, 103, 110, 101,
    100, 73, 110, 102, 111, 62, 60, 67, 97, 110, 111, 110, 105, 99, 97,
    108, 105, 122, 97, 116, 105, 111, 110, 77, 101, 116, 104, 111, 100,
    32, 65, 108, 103, 111, 114, 105, 116, 104, 109, 61, 34, 104, 116, 116,
    112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47,
    84, 82, 47, 50, 48, 48, 49, 47, 82, 69, 67, 45, 120, 109, 108, 45, 99,
    49, 52, 110, 45, 50, 48, 48, 49, 48, 51, 49, 53, 35, 87, 105, 116,
    104, 67, 111, 109, 109, 101, 110, 116, 115, 34, 62, 60, 47, 67, 97,
    110, 111, 110, 105, 99, 97, 108, 105, 122, 97, 116, 105, 111, 110, 77,
    101, 116, 104, 111, 100, 62, 60, 83, 105, 103, 110, 97, 116, 117, 114,
    101, 77, 101, 116, 104, 111, 100, 32, 65, 108, 103, 111, 114, 105,
    116, 104, 109, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119,
    46, 119, 51, 46, 111, 114, 103, 47, 50, 48, 48, 48, 47, 48, 57, 47,
    120, 109, 108, 100, 115, 105, 103, 35, 114, 115, 97, 45, 115, 104, 97,
    49, 34, 62, 60, 47, 83, 105, 103, 110, 97, 116, 117, 114, 101, 77,
    101, 116, 104, 111, 100, 62, 60, 82, 101, 102, 101, 114, 101, 110, 99,
    101, 32, 85, 82, 73, 61, 34, 34, 62, 60, 84, 114, 97, 110, 115, 102,
    111, 114, 109, 115, 62, 60, 84, 114, 97, 110, 115, 102, 111, 114, 109,
    32, 65, 108, 103, 111, 114, 105, 116, 104, 109, 61, 34, 104, 116, 116,
    112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47,
    50, 48, 48, 48, 47, 48, 57, 47, 120, 109, 108, 100, 115, 105, 103, 35,
    101, 110, 118, 101, 108, 111, 112, 101, 100, 45, 115, 105, 103, 110,
    97, 116, 117, 114, 101, 34, 62, 60, 47, 84, 114, 97, 110, 115, 102,
    111, 114, 109, 62, 60, 47, 84, 114, 97, 110, 115, 102, 111, 114, 109,
    115, 62, 60, 68, 105, 103, 101, 115, 116, 77, 101, 116, 104, 111, 100,
    32, 65, 108, 103, 111, 114, 105, 116, 104, 109, 61, 34, 104, 116, 116,
    112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47,
    50, 48, 48, 48, 47, 48, 57, 47, 120, 109, 108, 100, 115, 105, 103, 35,
    115, 104, 97, 49, 34, 62, 60, 47, 68, 105, 103, 101, 115, 116, 77,
    101, 116, 104, 111, 100, 62, 60, 68, 105, 103, 101, 115, 116, 86, 97,
    108, 117, 101, 62, 60, 47, 68, 105, 103, 101, 115, 116, 86, 97, 108,
    117, 101, 62, 60, 47, 82, 101, 102, 101, 114, 101, 110, 99, 101, 62,
    60, 47, 83, 105, 103, 110, 101, 100, 73, 110, 102, 111, 62, 60, 83,
    105, 103, 110, 97, 116, 117, 114, 101, 86, 97, 108, 117, 101, 62, 60,
    47, 83, 105, 103, 110, 97, 116, 117, 114, 101, 86, 97, 108, 117, 101,
    62, 60, 75, 101, 121, 73, 110, 102, 111, 62, 60, 88, 53, 48, 57, 68,
    97, 116, 97, 62, 60, 88, 53, 48, 57, 67, 101, 114, 116, 105, 102, 105,
    99, 97, 116, 101, 62, 60, 47, 88, 53, 48, 57, 67, 101, 114, 116, 105,
    102, 105, 99, 97, 116, 101, 62, 60, 47, 88, 53, 48, 57, 68, 97, 116,
    97, 62, 60, 47, 75, 101, 121, 73, 110, 102, 111, 62, 60, 47, 83, 105,
    103, 110, 97, 116, 117, 114, 101, 62]</p>
  </blockquote>
  
  <p>From that array i must obtain the bytes corresponding to the SignedInfo node. Those are the following bytes (I've bold barked the Middle Bytes of the SignedInfo Node):</p>
  
  <blockquote>
    <p>[60, 83, 105, 103, 110, 101, 100, 73, 110, 102, 111, 62, 60, 67, 97,
    110, 111, 110, 105, 99, 97, 108, 105, 122, 97, 116, 105, 111, 110, 77,
    101, 116, 104, 111, 100, 32, 65, 108, 103, 111, 114, 105, 116, 104,
    109, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119,
    51, 46, 111, 114, 103, 47, 84, 82, 47, 50, 48, 48, 49, 47, 82, 69, 67,
    45, 120, 109, 108, 45, 99, 49, 52, 110, 45, 50, 48, 48, 49, 48, 51,
    49, 53, 35, 87, 105, 116, 104, 67, 111, 109, 109, 101, 110, 116, 115,
    34, 62, 60, 47, 67, 97, 110, 111, 110, 105, 99, 97, 108, 105, 122, 97,
    116, 105, 111, 110, 77, 101, 116, 104, 111, 100, 62, 60, 83, 105, 103,
    110, 97, 116, 117, 114, 101, 77, 101, 116, 104, 111, 100, 32, 65, 108,
    103, 111, 114, 105, 116, 104, 109, 61, 34, 104, 116, 116, 112, 58, 47,
    47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47, 50, 48, 48, 48,
    47, 48, 57, 47, 120, 109, 108, 100, 115, 105, 103, 35, 114, 115, 97,
    45, 115, 104, 97, 49, 34, 62, 60, 47, 83, 105, 103, 110, 97, 116, 117,
    114, 101, 77, 101, 116, 104, 111, 100, 62, 60, 82, 101, 102, 101, 114,
    101, 110, 99, 101, 32, 85, 82, 73, 61, 34, 34, 62, 60, 84, 114, 97,
    110, 115, 102, 111, 114, 109, 115, 62, 60, 84, 114, 97, 110, 115, 102,
    111, 114, 109, 32, 65, 108, 103, 111, 114, 105, 116, 104, 109, 61, 34,
    104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111,
    114, 103, 47, 50, 48, 48, 48, 47, 48, 57, 47, 120, 109, 108, 100, 115,
    105, 103, 35, 101, 110, 118, 101, 108, 111, 112, 101, 100, 45, 115,
    105, 103, 110, 97, 116, 117, 114, 101, 34, 62, 60, 47, 84, 114, 97,
    110, 115, 102, 111, 114, 109, 62, 60, 47, 84, 114, 97, 110, 115, 102,
    111, 114, 109, 115, 62, 60, 68, 105, 103, 101, 115, 116, 77, 101, 116,
    104, 111, 100, 32, 65, 108, 103, 111, 114, 105, 116, 104, 109, 61, 34,
    104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111,
    114, 103, 47, 50, 48, 48, 48, 47, 48, 57, 47, 120, 109, 108, 100, 115,
    105, 103, 35, 115, 104, 97, 49, 34, 62, 60, 47, 68, 105, 103, 101,
    115, 116, 77, 101, 116, 104, 111, 100, 62, 60, 68, 105, 103, 101, 115,
    116, 86, 97, 108, 117, 101, <strong>62, 60</strong>, 47, 68, 105, 103, 101, 115,
    116, 86, 97, 108, 117, 101, 62, 60, 47, 82, 101, 102, 101, 114, 101,
    110, 99, 101, 62, 60, 47, 83, 105, 103, 110, 101, 100, 73, 110, 102,
    111, 62]</p>
  </blockquote>
  
  <p>To that byte array i should insert the Bytes corresponding to the Sha1 of the document encoded in B64.</p>
  
  <p>The Sha1 Bytes are</p>
  
  <blockquote>
    <p>[55, -59, -1, 71, -62, 26, 57, 126, 76, 7, 120, 53, -38, -51, 8, 38,
    127, -29, 5, 25]</p>
  </blockquote>
  
  <p><strong>And in B64</strong></p>
  
  <blockquote>
    <p>N8X/R8IaOX5MB3g12s0IJn/jBRk=</p>
  </blockquote>
  
  <p>And that back into bytes is:</p>
  
  <blockquote>
    <p>[78, 56, 88, 47, 82, 56, 73, 97, 79, 88, 53, 77, 66, 51, 103, 49, 50,
    115, 48, 73, 74, 110, 47, 106, 66, 82, 107, 61]</p>
  </blockquote>
  
  <p>The SignedInfo array with the hash attached then is:</p>
  
  <blockquote>
    <p>[60, 83, 105, 103, 110, 101, 100, 73, 110, 102, 111, 62, 60, 67, 97,
    110, 111, 110, 105, 99, 97, 108, 105, 122, 97, 116, 105, 111, 110, 77,
    101, 116, 104, 111, 100, 32, 65, 108, 103, 111, 114, 105, 116, 104,
    109, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119,
    51, 46, 111, 114, 103, 47, 84, 82, 47, 50, 48, 48, 49, 47, 82, 69, 67,
    45, 120, 109, 108, 45, 99, 49, 52, 110, 45, 50, 48, 48, 49, 48, 51,
    49, 53, 35, 87, 105, 116, 104, 67, 111, 109, 109, 101, 110, 116, 115,
    34, 62, 60, 47, 67, 97, 110, 111, 110, 105, 99, 97, 108, 105, 122, 97,
    116, 105, 111, 110, 77, 101, 116, 104, 111, 100, 62, 60, 83, 105, 103,
    110, 97, 116, 117, 114, 101, 77, 101, 116, 104, 111, 100, 32, 65, 108,
    103, 111, 114, 105, 116, 104, 109, 61, 34, 104, 116, 116, 112, 58, 47,
    47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47, 50, 48, 48, 48,
    47, 48, 57, 47, 120, 109, 108, 100, 115, 105, 103, 35, 114, 115, 97,
    45, 115, 104, 97, 49, 34, 62, 60, 47, 83, 105, 103, 110, 97, 116, 117,
    114, 101, 77, 101, 116, 104, 111, 100, 62, 60, 82, 101, 102, 101, 114,
    101, 110, 99, 101, 32, 85, 82, 73, 61, 34, 34, 62, 60, 84, 114, 97,
    110, 115, 102, 111, 114, 109, 115, 62, 60, 84, 114, 97, 110, 115, 102,
    111, 114, 109, 32, 65, 108, 103, 111, 114, 105, 116, 104, 109, 61, 34,
    104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111,
    114, 103, 47, 50, 48, 48, 48, 47, 48, 57, 47, 120, 109, 108, 100, 115,
    105, 103, 35, 101, 110, 118, 101, 108, 111, 112, 101, 100, 45, 115,
    105, 103, 110, 97, 116, 117, 114, 101, 34, 62, 60, 47, 84, 114, 97,
    110, 115, 102, 111, 114, 109, 62, 60, 47, 84, 114, 97, 110, 115, 102,
    111, 114, 109, 115, 62, 60, 68, 105, 103, 101, 115, 116, 77, 101, 116,
    104, 111, 100, 32, 65, 108, 103, 111, 114, 105, 116, 104, 109, 61, 34,
    104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111,
    114, 103, 47, 50, 48, 48, 48, 47, 48, 57, 47, 120, 109, 108, 100, 115,
    105, 103, 35, 115, 104, 97, 49, 34, 62, 60, 47, 68, 105, 103, 101,
    115, 116, 77, 101, 116, 104, 111, 100, 62, 60, 68, 105, 103, 101, 115,
    116, 86, 97, 108, 117, 101, <strong>62, 78, 56, 88, 47, 82, 56, 73, 97, 79,
    88, 53, 77, 66, 51, 103, 49, 50, 115, 48, 73, 74, 110, 47, 106, 66,
    82, 107, 61, 60</strong>, 47, 68, 105, 103, 101, 115, 116, 86, 97, 108, 117,
    101, 62, 60, 47, 82, 101, 102, 101, 114, 101, 110, 99, 101, 62, 60,
    47, 83, 105, 103, 110, 101, 100, 73, 110, 102, 111, 62]</p>
  </blockquote>
  
  <p>The above array, converted into string is:</p>
  
  <pre><code>&lt;SignedInfo&gt;&lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"&gt;&lt;/CanonicalizationMethod&gt;&lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"&gt;&lt;/SignatureMethod&gt;&lt;Reference URI=""&gt;&lt;Transforms&gt;&lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"&gt;&lt;/Transform&gt;&lt;/Transforms&gt;&lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"&gt;&lt;/DigestMethod&gt;&lt;DigestValue&gt;N8X/R8IaOX5MB3g12s0IJn/jBRk=&lt;/DigestValue&gt;&lt;/Reference&gt;&lt;/SignedInfo&gt;
  </code></pre>
  
  <p>Now, those are the bytes that (i think) should be RSA encrypted. I've applied several methods of signing:</p>
  
  <ol>
  <li>Java using Cipher class (Privatekey read through PEM fime)</li>
  <li>Java using Signature class (Privatekey read through PEM fime)</li>
  <li>C# using BouncyCastle libraries (in windows mobile emulator) (Key read through CertificateStore and transformed into BC format)</li>
  <li>C# using RSAFormatter (Key obtained through windows certificate store)</li>
  </ol>
  
  <p>And in every single one of them i got the same signature result for the bytes inputted:</p>
  
  <blockquote>
    <p>[2, -125, 23, -84, -28, -120, -45, -72, -73, -105, -71, -25, -100,
    -81, -77, 119, 98, 0, 28, -124, -92, 116, -108, -9, 22, -90, -103, -119, 52, 105, 53, 24, -59, -87, 25, -38, -31, -15, 39, 104, -4, 0, 62, -117, 103, -79, 112, 65, -43, -49, -26, -126, -108, 120, -4, -44,
    73, -33, 87, 39, 84, 7, 107, -81, 91, 61, -86, 100, 103, -112, -123,
    -118, 98, 85, -14, -88, -92, -45, -79, 3, -28, -18, 64, 2, -125, 53, -70, 100, -10, 86, -52, 17, -22, 110, -126, -100, -115, 45, -18, 99, -79, -92, -8, -120, -104, -63, 43, 70, -41, 98, 121, -68, -8, 60, -93, -95, -83, 83, -86, 75, -128, 120, -6, -11, 24, -124, 70, -128]</p>
    
    <p>AoMXrOSI07i3l7nnnK+zd2IAHISkdJT3FqaZiTRpNRjFqRna4fEnaPwAPotn
    sXBB1c/mgpR4/NRJ31cnVAdrr1s9qmRnkIWKYlXyqKTTsQPk7kACgzW6ZPZW
    zBHqboKcjS3uY7Gk+IiYwStG12J5vPg8o6GtU6pLgHj69RiERoA=</p>
  </blockquote>
  
  <p><strong>Since i get the same result for the same input data, the erorr is not in the signing procedure but in the data i'm inputting. And that's where i can't find a solution. By the way, the signature is valid if I validate it as a string with the corresponding public key or certificate... the error arises when i attach the signature node to the original document and validate it as an XMLDSIG.
  The signature takes basically three inputs
  Bytes to be signed
  Private Key
  Algorith
  The key and the algorith are ok (i think), the problem then needs to be in the bytes being signed, however i cant find what I'm doing wrong. If someone could give me a hand, i will be very grateful.</strong></p>
  
  <p>I'm including some code below to show the methods im using to get the same signature on the differents languages and devices.</p>
  
  <p>This is JAVA
      public static byte[] firmarSignedInfoconHash(byte[] bSignedInfo, PrivateKey privateKey, X509Certificate Certificado) throws Throwable
          {
              try
              {
                  String hexCan = bytesToHex(bSignedInfo);
                  String stringAFirmar = new String(bSignedInfo, "ISO-8859-1");</p>
  
  <pre><code>            //ONE WAY OF SIGNING
              byte[] signedInfoSha1Digest = sha1Digest(bSignedInfo);
              //byte[] bytesCS = new byte[]{55,-59,-1,71,-62,26,57,126,76,7,120,53,-38,-51,8,38,127,-29,5,25};
              String vSignedInfoSha1DigestString64 = Base64.encodeToString(signedInfoSha1Digest, Base64.DEFAULT);
              byte[] signedInfoDerSha1Digest = mergeArrays(DER_SHA1_DIGEST_IDENTIFIER, signedInfoSha1Digest);
              Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding","BC");
              cipher.init(Cipher.ENCRYPT_MODE, privateKey);
              byte[] signatureBytes = cipher.doFinal(signedInfoDerSha1Digest);
              String base64RsaSignature1 = base64encode(signatureBytes, true);
              //String vFirma = bytesToHex(signatureBytes);
  
              //ANOTHER WAY OF SIGNING
              Signature instance2 = Signature.getInstance("SHA1withRSA");
              instance2.initSign(privateKey);
              instance2.update(bSignedInfo);
              byte[] bFirma3 = instance2.sign();
              String base64RsaSignature2 = base64encode(bFirma3, true);
              Log.i("Log","nada");
  
              //VALIDATE THE RESULT
              Signature instanceValida = Signature.getInstance("SHA1withRSA");
              instanceValida.initVerify(Certificado);
              instanceValida.update(bSignedInfo);
              if(instanceValida.verify(bFirma3)==true)
                  Log.i("Validacion","La firma es valida");
              else
                  Log.i("Validacion","La firma NO ES valida");
  
              return bFirma3;
          } 
          catch (Throwable e) {
              Log.e(LOG_TAG, "Error generating signature for XML", e);
              throw e;
          }
      }
  </code></pre>
  
  <p>This is C#</p>
  
  <pre><code>public static void SignXmlFile3(string Cadena, X509Certificate2 Key)
          {
              /*opcion 1
              RSACryptoServiceProvider RSA = Key.PrivateKey as RSACryptoServiceProvider;
              RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
               * */
  
              //Opcion 2
              RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter();
              RSAFormatter.SetKey(Key.PrivateKey as RSACryptoServiceProvider);
              RSAFormatter.SetHashAlgorithm("SHA1");
              SHA1Managed SHhash = new SHA1Managed();
              var bytes3 = Encoding.UTF8.GetBytes(Cadena);
              var bytes = Convert.FromBase64String(Cadena);
              byte[] SignedHashValue = RSAFormatter.CreateSignature(SHhash.ComputeHash(bytes));
              string signature = System.Convert.ToBase64String(SignedHashValue);
  
  
          }
  </code></pre>
  
  <p>This is c# using bouncyCastle on WindowsPhone</p>
  
  <pre><code>public String Sign(String data, String privateModulusHexString, String privateExponentHexString, X509Certificate2 vX509Certificate2)
          {
              //var test = DotNetUtilities.FromX509Certificate(vX509Certificate2);
  
              /* Make the key */
              RsaKeyParameters key = MakeKey(privateModulusHexString, privateExponentHexString, true);
  
              /* Init alg */
              ISigner sig = SignerUtilities.GetSigner("SHA1withRSA");
  
              /* Populate key */
              sig.Init(true, key);
  
              /* Get the bytes to be signed from the string */
              var bytes = Encoding.UTF8.GetBytes(data);
  
              /* Calc the signature */
              sig.BlockUpdate(bytes, 0, bytes.Length);
              byte[] signature = sig.GenerateSignature();
  
              /* Base 64 encode the sig so its 8-bit clean */
              var signedString = Convert.ToBase64String(signature);
  
              return signedString;
          }
  </code></pre>