【问题标题】:Add public key to the signature of an xml document将公钥添加到 xml 文档的签名中
【发布时间】:2019-10-29 14:14:57
【问题描述】:

我想在Digital Signature 中添加包含public key 的其他RsaKeyValue KeyInfo

然后用户不必保存certificate - 而是可以使用public key 来检查文档的有效性。

到目前为止,这是我的签名功能:

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));
   }

在本文档中它是用 C++ 编写的: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.xml.rsakeyvalue?view=netframework-4.8

向下滚动做部分

// 添加一个 RSAKeyValue KeyInfo(可选;帮助收件人找到要验证的密钥)。

如何在 C# 中做到这一点?

如何将这个可选的密钥信息与公钥一起添加?

【问题讨论】:

标签: c# c++ xml signature


【解决方案1】:

经过大量挖掘和尝试,我解决了它。在签名中发送公钥是我所做的:

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

这会将以下内容添加到 XML 中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2011-01-02
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    相关资源
    最近更新 更多