gpt4 book ai didi

c# - 我们如何读取 X509 证书并将其附加到 Xml 数字签名?

转载 作者:行者123 更新时间:2023-11-30 22:16:22 25 4
gpt4 key购买 nike

我想从 TrustedPeople 商店读取 x509 证书并将其附加到 XML 文档。

            RSAKeyValue rsaKey = new RSAKeyValue();
XmlDocument xmlDoc = new XmlDocument();
string filename = "C:/Documents and Settings/sbtho/Desktop/downloads/samp.xml";
string filename1 = "C:/Documents and Settings/sbtho/Desktop/downloads/sampdigsig.xml";
xmlDoc.PreserveWhitespace = false;
xmlDoc.Load(new XmlTextReader(filename));

SignedXml signedXml = new SignedXml(xmlDoc);
signedXml.SigningKey = rsaKey.Key;
Signature xmlSignature = signedXml.Signature;

Reference reference = new Reference("");
XmlDsigEnvelopedSignatureTransform envelope = new XmlDsigEnvelopedSignatureTransform();
XmlDsigC14NWithCommentsTransform envelope1 = new XmlDsigC14NWithCommentsTransform();
reference.AddTransform(envelope);
reference.AddTransform(envelope1);
xmlSignature.SignedInfo.AddReference(reference);

KeyInfo keyInfo = new KeyInfo();
X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, "ffa8ebf4760ab2d145b8ca21b1de258923e7d9d8", false);
store.Close();
keyInfo.AddClause(rsaKey);
xmlSignature.KeyInfo = keyInfo;
signedXml.ComputeSignature();

XmlElement xmlDigSign = signedXml.GetXml();
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigSign, true));

if (xmlDoc.FirstChild.GetType() == typeof(XmlDeclaration))
xmlDoc.RemoveChild(xmlDoc.FirstChild);

XmlTextWriter xmlWriter = new XmlTextWriter(filename1, new UTF8Encoding(false));
xmlDoc.WriteTo(xmlWriter);
xmlWriter.Close();

这是我到达的距离。它是从 trustedpeople 商店读取的。现在如何将此证书插入 XML 文档?

最佳答案

这里x509证书存储在x509certificate2collection对象类型中,显示应该存储在x509证书对象类型中

 X509Certificate2 cer=new X509Certificate2();
if (certs.Count > 0)
{
cer = certs[0];
};

现在可以使用 keyinfo addclause 将其添加到 xml 签名文档中。

 keyInfo.AddClause(new KeyInfoX509Data(cer));

关于c# - 我们如何读取 X509 证书并将其附加到 Xml 数字签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17440722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com