Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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# 如何设置XMLDSIGenveledSignatureTransform的摘要和签名方法_C#_Hash_Digital Signature_Sha1_Xml Signature - Fatal编程技术网

C# 如何设置XMLDSIGenveledSignatureTransform的摘要和签名方法

C# 如何设置XMLDSIGenveledSignatureTransform的摘要和签名方法,c#,hash,digital-signature,sha1,xml-signature,C#,Hash,Digital Signature,Sha1,Xml Signature,我目前正在尝试使用提供的p12文件对xml文档进行签名,我想我已经成功地对它进行了签名,但是在通过SOAP发送文档之后,我总是得到“无效签名”的响应。结果xml本身具有与运算符示例中提供的结构相同的结构-因此唯一可能出错的是我的签名值。最让我困扰的是,在我的代码中设置SignatureMethod和DigestMethod完全是基于放置几个URI值——因此我的问题是——我做得对吗(因为我必须使用SHA1,而不是默认的SHA256)?。用于对XML进行签名的方法: 另外,uidCert参数创建如下

我目前正在尝试使用提供的p12文件对xml文档进行签名,我想我已经成功地对它进行了签名,但是在通过SOAP发送文档之后,我总是得到“无效签名”的响应。结果xml本身具有与运算符示例中提供的结构相同的结构-因此唯一可能出错的是我的签名值。最让我困扰的是,在我的代码中设置SignatureMethod和DigestMethod完全是基于放置几个URI值——因此我的问题是——我做得对吗(因为我必须使用SHA1,而不是默认的SHA256)?。用于对XML进行签名的方法:

另外,uidCert参数创建如下:
X509Certificate2 uidCert=new X509Certificate2(“file.p12”,“pass”)


非常感谢您的帮助,因为我不是XML或数字签名方面的专家。顺便说一句,在旁注中-如何将X509IssuerSerial标记放在签名上的X509Certificate标记之后?

查看以下帮助:@jdweng我不确定这如何适用于我的特定问题,因为他们在特定示例中使用了默认的SHA256?或者我遗漏了什么?事实上,我的错是,他们使用的是SHA1,但如果我没有错的话,X509的默认值……取决于证书的类型。SSL、TLS 1.0、TLS 1.1不再被使用,因为黑客发现他们破坏了加密。因此,自今年6月起,建议仅使用TLS 1.2或TLS 1.3。请参阅Wiki以了解不同版本的SSL/TLS使用了哪些加密模式:@jdweng感谢您指出,my文档要求我使用TLS 1.1或1.2,而我目前还没有使用过TLS 1.1或1.2,但仍然需要SHA1进行加密,我不知道我是否以这种方式设置,因此问题没有得到解决:(
public static XDocument SignXml(XmlDocument xmlDoc, X509Certificate2 uidCert)
    {
        RSA rsaKey = (RSA)uidCert.PrivateKey;

        // Check arguments. 
        if (xmlDoc == null)
            throw new ArgumentException("xmlDoc");
        if (rsaKey == null)
            throw new ArgumentException("Key");

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

        // Add the key to the SignedXml document.
        signedXml.SigningKey = rsaKey;
        signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url;


        // Specify a canonicalization method.
        signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;


        // Create a reference to be signed.
        Reference reference = new Reference();
        reference.Uri = "#billId";
        reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";
        // Add an enveloped transformation to the reference.
        XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);
        reference.AddTransform(new XmlDsigExcC14NTransform());
        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);


        // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
        KeyInfo keyInfo = new KeyInfo();

        KeyInfoX509Data clause = new KeyInfoX509Data();
        clause.AddCertificate(uidCert);
        clause.AddIssuerSerial(uidCert.IssuerName.Name, uidCert.SerialNumber);
        keyInfo.AddClause(clause);

        signedXml.KeyInfo = keyInfo;

        // Compute the signature.
        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.
        xmlDoc.GetElementsByTagName("tns:BillRequest").Item(0).AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));

        return ToXDocument(xmlDoc);
    }