Skip to main content
se añadieron 252 caracteres en el cuerpo
Origen Enlace
Miguel Zarate
  • 2.4k
  • 1
  • 11
  • 30

private static void FirmarDocumento(string pathXmlDocument,string pathCert,string passCert, string pathXmlSignet) {

private static void FirmarDocumento(string pathXmlDocument,string pathCert,string passCert, string pathXmlSignet)
    {


        XmlDocument documentXml = new XmlDocument();
        documentXml.Load(pathXmlDocument);

        SignedXml firmado = new SignedXml(documentXml);

        var cert = RSA_helper.GetX509Certificate(pathCert, passCert);
        firmado.SigningKey = (RSA)cert.PrivateKey;
        firmado.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";

        //digest info
        Reference reference = new Reference();
        //reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        //reference.AddTransform(new XmlDsigExcC14NTransform());
        reference.Uri = "";
  
          
              reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";

        firmado.AddReference(reference);

        // with the public key will be added in the signature part. 
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new RSAKeyValue((RSA)cert.PrivateKey));
        keyInfo.AddClause(new KeyInfoX509Data(cert));
   
        firmado.KeyInfo = keyInfo;


        firmado.ComputeSignature();

        XmlElement xmlDigitalSignature = firmado.GetXml();
        documentXml.DocumentElement.AppendChild(documentXml.ImportNode(xmlDigitalSignature, true));
        //guardando salida.
        documentXml.Save(pathXmlSignet);
    }
 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
          <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
          <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
          <Reference URI="">
            <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
            <DigestValue>ooqJ2kDYWTdeb+nrSvsUYq7KNys=</DigestValue>
          </Reference>
        </SignedInfo>
        <SignatureValue>0Qp1N1+VPW594eI/AIgeKs2A6ZcglixEDrVPxCSCID0XXG2u8i2Xz4uGbYTcRvCvZWoG7wK7dAMc9nH8CZtnBgvQcF4EUYkuMU12wgGl2Gszk+0CM3rAJ3TGx5wpwfcWB97O7xJcxdWcE/ERPXXjR5gjXCl3KaVfbDuZTfj79/zHJvfUQjTA7DfsOSF4RxDsBg+ZWZbZNHarUIfEhqO3dAWfQSrvymtRNuRn1Z+arCgrTzhCKdQH4oan0In7+ap0RLfMrYVSE4nOjwbyc9ud11aD4ZMkKYP9oFbIPYlBI3z3kVp5k4pppWnLTJBACokmEujt+zGSWJhRD0//2baz7Q==</SignatureValue>
        <KeyInfo>
          <KeyValue>
            <RSAKeyValue>
              <Modulus>4iM7/CK3bQmQn2bAWbN/nxj8jJIgV/AQBFFujeTq3frI+4eEJhtLPws6ATPrQT9kSgPViIZUyak3TJNhiZovrQ0uOXyxEqR8Z5W8Lza/dNzHYgcTjHE3hfc664hOH3YeIixllr09LFMEcwbVwJB7GnuRpy9lvxHKOtQ2E+3AVvjZRmonfBx+7+paAk5zE2U/yhePVTubww/+uxDGNToO+0ER4I+aQ8Tk4RDJl3SKt9YCYc5J0E4Er1MFlRunwFrSjKAg/mNxwYuumxDDzIpwFhi93KSOJHA2kQRqLOZgCtkJT3EVUeojAHY/0pvkC9CT5zUkcWGfTfqFkyXnsnnPmQ==</Modulus>
              <Exponent>AQAB</Exponent>
            </RSAKeyValue>
          </KeyValue>
          <X509Data>
            <X509Certificate></X509Certificate>
          </X509Data>
        </KeyInfo>
      </Signature>

private static void FirmarDocumento(string pathXmlDocument,string pathCert,string passCert, string pathXmlSignet) {

    XmlDocument documentXml = new XmlDocument();
    documentXml.Load(pathXmlDocument);

    SignedXml firmado = new SignedXml(documentXml);

    var cert = RSA_helper.GetX509Certificate(pathCert, passCert);
    firmado.SigningKey = (RSA)cert.PrivateKey;
    firmado.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";

    //digest info
    Reference reference = new Reference();
    //reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
    //reference.AddTransform(new XmlDsigExcC14NTransform());
    reference.Uri = "";

      
          reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";

    firmado.AddReference(reference);

    // with the public key will be added in the signature part. 
    KeyInfo keyInfo = new KeyInfo();
    keyInfo.AddClause(new RSAKeyValue((RSA)cert.PrivateKey));
    keyInfo.AddClause(new KeyInfoX509Data(cert));

    firmado.KeyInfo = keyInfo;


    firmado.ComputeSignature();

    XmlElement xmlDigitalSignature = firmado.GetXml();
    documentXml.DocumentElement.AppendChild(documentXml.ImportNode(xmlDigitalSignature, true));
    //guardando salida.
    documentXml.Save(pathXmlSignet);
}
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
      <Reference URI="">
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>ooqJ2kDYWTdeb+nrSvsUYq7KNys=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>0Qp1N1+VPW594eI/AIgeKs2A6ZcglixEDrVPxCSCID0XXG2u8i2Xz4uGbYTcRvCvZWoG7wK7dAMc9nH8CZtnBgvQcF4EUYkuMU12wgGl2Gszk+0CM3rAJ3TGx5wpwfcWB97O7xJcxdWcE/ERPXXjR5gjXCl3KaVfbDuZTfj79/zHJvfUQjTA7DfsOSF4RxDsBg+ZWZbZNHarUIfEhqO3dAWfQSrvymtRNuRn1Z+arCgrTzhCKdQH4oan0In7+ap0RLfMrYVSE4nOjwbyc9ud11aD4ZMkKYP9oFbIPYlBI3z3kVp5k4pppWnLTJBACokmEujt+zGSWJhRD0//2baz7Q==</SignatureValue>
    <KeyInfo>
      <KeyValue>
        <RSAKeyValue>
          <Modulus>4iM7/CK3bQmQn2bAWbN/nxj8jJIgV/AQBFFujeTq3frI+4eEJhtLPws6ATPrQT9kSgPViIZUyak3TJNhiZovrQ0uOXyxEqR8Z5W8Lza/dNzHYgcTjHE3hfc664hOH3YeIixllr09LFMEcwbVwJB7GnuRpy9lvxHKOtQ2E+3AVvjZRmonfBx+7+paAk5zE2U/yhePVTubww/+uxDGNToO+0ER4I+aQ8Tk4RDJl3SKt9YCYc5J0E4Er1MFlRunwFrSjKAg/mNxwYuumxDDzIpwFhi93KSOJHA2kQRqLOZgCtkJT3EVUeojAHY/0pvkC9CT5zUkcWGfTfqFkyXnsnnPmQ==</Modulus>
          <Exponent>AQAB</Exponent>
        </RSAKeyValue>
      </KeyValue>
      <X509Data>
        <X509Certificate></X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
private static void FirmarDocumento(string pathXmlDocument,string pathCert,string passCert, string pathXmlSignet)
    {


        XmlDocument documentXml = new XmlDocument();
        documentXml.Load(pathXmlDocument);

        SignedXml firmado = new SignedXml(documentXml);

        var cert = RSA_helper.GetX509Certificate(pathCert, passCert);
        firmado.SigningKey = (RSA)cert.PrivateKey;
        firmado.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";

        //digest info
        Reference reference = new Reference();
        //reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        //reference.AddTransform(new XmlDsigExcC14NTransform());
        reference.Uri = "";
  
          
              reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";

        firmado.AddReference(reference);

        // with the public key will be added in the signature part. 
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new RSAKeyValue((RSA)cert.PrivateKey));
        keyInfo.AddClause(new KeyInfoX509Data(cert));
   
        firmado.KeyInfo = keyInfo;


        firmado.ComputeSignature();

        XmlElement xmlDigitalSignature = firmado.GetXml();
        documentXml.DocumentElement.AppendChild(documentXml.ImportNode(xmlDigitalSignature, true));
        //guardando salida.
        documentXml.Save(pathXmlSignet);
    }
 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
          <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
          <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
          <Reference URI="">
            <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
            <DigestValue>ooqJ2kDYWTdeb+nrSvsUYq7KNys=</DigestValue>
          </Reference>
        </SignedInfo>
        <SignatureValue>0Qp1N1+VPW594eI/AIgeKs2A6ZcglixEDrVPxCSCID0XXG2u8i2Xz4uGbYTcRvCvZWoG7wK7dAMc9nH8CZtnBgvQcF4EUYkuMU12wgGl2Gszk+0CM3rAJ3TGx5wpwfcWB97O7xJcxdWcE/ERPXXjR5gjXCl3KaVfbDuZTfj79/zHJvfUQjTA7DfsOSF4RxDsBg+ZWZbZNHarUIfEhqO3dAWfQSrvymtRNuRn1Z+arCgrTzhCKdQH4oan0In7+ap0RLfMrYVSE4nOjwbyc9ud11aD4ZMkKYP9oFbIPYlBI3z3kVp5k4pppWnLTJBACokmEujt+zGSWJhRD0//2baz7Q==</SignatureValue>
        <KeyInfo>
          <KeyValue>
            <RSAKeyValue>
              <Modulus>4iM7/CK3bQmQn2bAWbN/nxj8jJIgV/AQBFFujeTq3frI+4eEJhtLPws6ATPrQT9kSgPViIZUyak3TJNhiZovrQ0uOXyxEqR8Z5W8Lza/dNzHYgcTjHE3hfc664hOH3YeIixllr09LFMEcwbVwJB7GnuRpy9lvxHKOtQ2E+3AVvjZRmonfBx+7+paAk5zE2U/yhePVTubww/+uxDGNToO+0ER4I+aQ8Tk4RDJl3SKt9YCYc5J0E4Er1MFlRunwFrSjKAg/mNxwYuumxDDzIpwFhi93KSOJHA2kQRqLOZgCtkJT3EVUeojAHY/0pvkC9CT5zUkcWGfTfqFkyXnsnnPmQ==</Modulus>
              <Exponent>AQAB</Exponent>
            </RSAKeyValue>
          </KeyValue>
          <X509Data>
            <X509Certificate></X509Certificate>
          </X509Data>
        </KeyInfo>
      </Signature>
Origen Enlace
Miguel Zarate
  • 2.4k
  • 1
  • 11
  • 30

Sebas solo elimina los elementos de transformacion, estos no son necesarios para la verificacion de firma, tambien el contenido del agregar parte del certificado puedes excluirlo, aqui el metodo con algunos elementos eliminados y agregada la parte de RSAKeyValue

private static void FirmarDocumento(string pathXmlDocument,string pathCert,string passCert, string pathXmlSignet) {

    XmlDocument documentXml = new XmlDocument();
    documentXml.Load(pathXmlDocument);

    SignedXml firmado = new SignedXml(documentXml);

    var cert = RSA_helper.GetX509Certificate(pathCert, passCert);
    firmado.SigningKey = (RSA)cert.PrivateKey;
    firmado.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";

    //digest info
    Reference reference = new Reference();
    //reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
    //reference.AddTransform(new XmlDsigExcC14NTransform());
    reference.Uri = "";

      
          reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";

    firmado.AddReference(reference);

    // with the public key will be added in the signature part. 
    KeyInfo keyInfo = new KeyInfo();
    keyInfo.AddClause(new RSAKeyValue((RSA)cert.PrivateKey));
    keyInfo.AddClause(new KeyInfoX509Data(cert));

    firmado.KeyInfo = keyInfo;


    firmado.ComputeSignature();

    XmlElement xmlDigitalSignature = firmado.GetXml();
    documentXml.DocumentElement.AppendChild(documentXml.ImportNode(xmlDigitalSignature, true));
    //guardando salida.
    documentXml.Save(pathXmlSignet);
}

con esto obtienes una salida similar a lo que buscas:

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
      <Reference URI="">
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>ooqJ2kDYWTdeb+nrSvsUYq7KNys=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>0Qp1N1+VPW594eI/AIgeKs2A6ZcglixEDrVPxCSCID0XXG2u8i2Xz4uGbYTcRvCvZWoG7wK7dAMc9nH8CZtnBgvQcF4EUYkuMU12wgGl2Gszk+0CM3rAJ3TGx5wpwfcWB97O7xJcxdWcE/ERPXXjR5gjXCl3KaVfbDuZTfj79/zHJvfUQjTA7DfsOSF4RxDsBg+ZWZbZNHarUIfEhqO3dAWfQSrvymtRNuRn1Z+arCgrTzhCKdQH4oan0In7+ap0RLfMrYVSE4nOjwbyc9ud11aD4ZMkKYP9oFbIPYlBI3z3kVp5k4pppWnLTJBACokmEujt+zGSWJhRD0//2baz7Q==</SignatureValue>
    <KeyInfo>
      <KeyValue>
        <RSAKeyValue>
          <Modulus>4iM7/CK3bQmQn2bAWbN/nxj8jJIgV/AQBFFujeTq3frI+4eEJhtLPws6ATPrQT9kSgPViIZUyak3TJNhiZovrQ0uOXyxEqR8Z5W8Lza/dNzHYgcTjHE3hfc664hOH3YeIixllr09LFMEcwbVwJB7GnuRpy9lvxHKOtQ2E+3AVvjZRmonfBx+7+paAk5zE2U/yhePVTubww/+uxDGNToO+0ER4I+aQ8Tk4RDJl3SKt9YCYc5J0E4Er1MFlRunwFrSjKAg/mNxwYuumxDDzIpwFhi93KSOJHA2kQRqLOZgCtkJT3EVUeojAHY/0pvkC9CT5zUkcWGfTfqFkyXnsnnPmQ==</Modulus>
          <Exponent>AQAB</Exponent>
        </RSAKeyValue>
      </KeyValue>
      <X509Data>
        <X509Certificate></X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>