简体   繁体   English

将公钥添加到 xml 文档的签名中

[英]Add public key to the signature of an xml document

I would like to add additional RsaKeyValue KeyInfo that includes the public key in the Digital Signature .我想添加额外的RsaKeyValue KeyInfo ,其中包括Digital Signature中的public key

The user then does not have to save the certificate - instead he can use that public key to check the validity of the document.然后,用户不必保存certificate ——相反,他可以使用该public key来检查文档的有效性。

So far here is my signing function:到目前为止,这是我签署的 function:

public static void SignXmlDocumentWithCertificate(XmlDocument xmlDoc, X509Certificate2 cert)
    {
        SignedXml signedXml = new SignedXml(xmlDoc);
        //we will sign it with private key
        signedXml.SigningKey = cert.PrivateKey;
        Reference reference = new Reference();
        //sign the entire doc
        reference.Uri = "";
        XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);
        signedXml.AddReference(reference);

        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new KeyInfoX509Data(cert));
        keyInfo.(cert);
        signedXml.KeyInfo = keyInfo;
        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.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
   }

It is written in C++ in this document: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.xml.rsakeyvalue?view=netframework-4.8在本文档中用 C++ 编写: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.Z0F635D0E0F3874FFF8B581C132E6C7A7-Zrsavalue?

Scroll down do the part向下滚动做部分

// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). // 添加一个 RSAKeyValue KeyInfo(可选;帮助收件人找到要验证的密钥)。

How do I do it in C#?我如何在 C# 中做到这一点?

How do I add this optional keyinfo with the public key?如何使用公钥添加此可选密钥信息?

I solved it after a lot of digging and trying.经过大量的挖掘和尝试,我解决了它。 To send the public key in the Signature here is what I did:在签名中发送公钥是我所做的:

RSACryptoServiceProvider rsaprovider = (RSACryptoServiceProvider)cert.PublicKey.Key;
RSAKeyValue rkv = new RSAKeyValue(rsaprovider);
keyInfo.AddClause(rkv);

This add the following to the XML:这会将以下内容添加到 XML:

        <KeyValue>
        <RSAKeyValue>
        <Modulus>t++UmV1G9ApuI118GdwK0BoxN3tjrxuQHTwKvlFgl6VrcLhMCb5Q2prga8I4HKLvLDr3L4bsrH0k9r6PPppqMpiN/KGdm6eB2uLnWtJXh1PWcnzfHfodYfQP/NAavIo4wSjss0L41c75/CA0x11iDdU4BOdTHGXaFCaNPQ5DLe3LK+6hjZ+fOYMpCd035TYTLo5+/Ttk5eCzr+MHfnWCaCIOUgkbq0OIUQWch2Sc9regIiA9oPPjUmmbqptLfm9wZBHRZZ+7Q4BewxSBBCIFt5yPhCsTZ1fFINV16tGtXTmtgXCagu4NiH7XsyhZhYDrA8CXb31Dn7M/ussNQkGrEQ==
        </Modulus>
        <Exponent>AQAB</Exponent>
        </RSAKeyValue>
        </KeyValue>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM