Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为xml元素c签名#_C#_Xml_Element_Sign - Fatal编程技术网

C# 为xml元素c签名#

C# 为xml元素c签名#,c#,xml,element,sign,C#,Xml,Element,Sign,您能告诉我如何在C#中为xml元素签名吗 使用System.Security.Cryptography 使用System.Security.Cryptography.Xml 使用System.Security.Cryptography.X509证书 例如: 我有一个xml文件: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Signature

您能告诉我如何在C#中为xml元素签名吗

使用System.Security.Cryptography

使用System.Security.Cryptography.Xml

使用System.Security.Cryptography.X509证书

例如:

我有一个xml文件:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Signature Id="SignatureIdValue" 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="#idPackageObject" Type="http://www.w3.org/2000/09/xmldsig#Object">
            <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
            <DigestValue>3H+EGzfJMnudlkWAtFYTfJkaeZM=</DigestValue>
        </Reference>
    </SignedInfo>
    <SignatureValue>h7ApS9H4NagiJIvt9xUy9FijPVpSQQQtUtvn/hU/WuSPPqap4r3NK98K+qTKptCPTgXcY3P3o+l+vrEXnl71gttfvK3nQabNtPlaXd5KR7fLAJq+6xJNzznLFu7d4JmXDYN3xfq7Scr+vlWcaU5zIGBBbIg90w3AXe1GsYRCpME=</SignatureValue>
    <Object Id="idPackageObject">
        <Manifest>
            <Reference URI="/finder.xml?ContentType=vnd-sizr-datacollection/finder">
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>pQAvJzZlmBqHmPU46dj4rYQqjPM=</DigestValue>
            </Reference>
            <Reference URI="/_rels/finder.xml.rels?ContentType=application/vnd.openxmlformats-package.relationships+xml">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>Qcp4TAsGEpSIhnVDCYCKih3t+tg=</DigestValue>
            </Reference>
            <Reference URI="/content.xml?ContentType=vnd-sizr-datacollection/content">
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>i8TcHWdSKqLEpMevvhRztwrFCO4=</DigestValue>
            </Reference>
            <Reference URI="/systemcheck.xml?ContentType=vnd-sizr-datacollection/systemcheck">
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>NB1XkMlRU83JUjZqdZLJ0925T54=</DigestValue>
            </Reference>
            <Reference URI="tree/service.xml?ContentType=vnd-sizr-datacollection/service">
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>4FgBGSm/TosmN5bngmTKapOHMSc=</DigestValue>
            </Reference>
        </Manifest>
        <SignatureProperties>
            <SignatureProperty Id="idSignatureTime" Target="#SignatureIdValue">
                <SignatureTime xmlns="http://schemas.openxmlformats.org/package/2006/digital-signature">
                    <Format>YYYY-MM-DDThh:mm:ss.sTZD</Format>
                    <Value>2018-03-25T01:07:44.0+00:00</Value>
                </SignatureTime>
            </SignatureProperty>
        </SignatureProperties>
    </Object>
</Signature>
但是我不知道如何使用

请帮助我。

请在下面的帖子中查看我的解决方案:谢谢@jdweng,我已经找到了这个问题的解决方案:)。
private static void SignObject(ref XmlDocument xmlDoc)
    {
        // Generate a signing key.
        RSACryptoServiceProvider Key = new RSACryptoServiceProvider();

        // Create a SignedXml object.
        SignedXml signedXml = new SignedXml();

        // Add the key to the SignedXml document. 
        signedXml.SigningKey = Key;

        // Create a reference to be signed.
        Reference reference = new Reference();
        
        reference.Uri = "";

        // Add an enveloped transformation to the reference.
        reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());

        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        try
        {
            // Create a new KeyInfo object.
            KeyInfo keyInfo = new KeyInfo();

            // Load the X509 certificate.
            X509Certificate MSCert =
                X509Certificate.CreateFromCertFile(Certificate);

            // Load the certificate into a KeyInfoX509Data object
            // and add it to the KeyInfo object.
            keyInfo.AddClause(new KeyInfoX509Data(MSCert));

            // Add the KeyInfo object to the SignedXml object.
            signedXml.KeyInfo = keyInfo;
        }
        catch (FileNotFoundException ex)
        {
            Console.WriteLine("Unable to locate the following file: " +
                Certificate);
        }

        // Compute the signature.
        signedXml.ComputeSignature();

        // Add the signature branch to the original tree so it is enveloped.
        xmlDoc.DocumentElement.AppendChild(signedXml.GetXml());
    }