【问题标题】:How do we read and attach a X509 certificate to Xml Digital Signature?我们如何读取 X509 证书并将其附加到 Xml 数字签名?
【发布时间】:2013-07-03 05:59:23
【问题描述】:

我想从 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 文档?

【问题讨论】:

    标签: c# xml x509certificate digital-signature x509certificate2


    【解决方案1】:

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

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

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

     keyInfo.AddClause(new KeyInfoX509Data(cer));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 2013-06-28
      • 2012-05-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2015-03-03
      相关资源
      最近更新 更多