提取与X509证书XML文件中的公钥? [英] Extract public key from an XML file with X509certificate?

查看:996
本文介绍了提取与X509证书XML文件中的公钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从XML文件创建在C#中的 X509Certificate2 对象。 XML文件是我们从供应商那里获得了 SAML 元数据文件。

I am trying to create an X509Certificate2 object in C# from an XML file. The XML file is a SAML metadata file that we received from a vendor.

我试图提取公从这些XML元素键:

I am trying to extract the public key from these XML Elements:

<X509Data>
    <X509Certificate>
      MIIB7DCCAVmgAwIBAgIQPjHcBTL63bBLuJZ88RcrCjAJBgUrDgMCHQUAMBExDzANBgNVBAMT
      BnJvbWVvazAgFw0xMDAzMTUwMjI1MjZaGA8yMTEwMDIxOTAyMjUyNlowETEPMA0GA1UEAxMG
      cm9tZW9rMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAu/sBh13A27rR7gJpZsI6zCee
      TXNohQWlq2z6Zg8Oxzsy5JoVV 
    </X509Certificate>
</X509Data> 



有没有在C#的方式来提取无论是从XML元素的.CER文件或公钥呢?

Is there a way in C# to extract either the .cer file or public key from the XML element?

推荐答案

这是一个很难回答的问题不知道X509证书是如何编码的回答,但假设你有编码的东西,你可以做类似如下:

This is a difficult question to answer without knowing how the X509Certificate is encoded, but assuming you have the encoding stuff, you can do something like the following:

  var document = new XmlDocument();
  document.LoadXml(txtXml.Text);
  var cert = document.SelectSingleNode("X509Data/X509Certificate").InnerText;
  /*...Decode text in cert here (may need to use Encoding, Base64, UrlEncode, etc) ending with 'data' being a byte array...*/ 
  var x509 = new X509Certificate2(data);



然后,你应该能够使用标准的文件I / O逻辑将文件写入到磁盘。

Then you should be able to write the file to disk using standard File I/O logic.

这篇关于提取与X509证书XML文件中的公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆