简体   繁体   English

如何从C#中的xml数字签名(.cer文件)获取信息?

[英]How to obtain information from xml digital signature (.cer file) in C#?

I am using service that returns the XML signature. 我正在使用返回XML签名的服务。 now my task is to identify the signer name from response xml signature. 现在,我的任务是从响应xml签名中识别签名者名称。

XML response signature format : XML响应签名格式:

<?xml version="1.0" encoding="UTF-8"?>
<EsignResp errCode="NA" errMsg="NA" resCode="XXXXXXXXXXXXXXXXXXXXXXXX" status="1" ts="2019-05-02T15:15:13" txn="XXXXXXXXXXXXXXXXXXXXXXXX">
   <UserX509Certificate>XXXXXXXXXXXXXXXXXXXXXXXX</UserX509Certificate>
   <Signatures>
      <DocSignature error="" id="1" sigHashAlgorithm="SHA256">XXXXXXXXXXXXXXXXXXXXXXXX</DocSignature>
   </Signatures>
   <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
      <SignedInfo>
         <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
         <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
         <Reference URI="">
            <Transforms>
               <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
            </Transforms>
            <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
            <DigestValue>XXXXXXXXXXXXXXXXXXXXXXXX</DigestValue>
         </Reference>
      </SignedInfo>
      <SignatureValue>XXXXXXXXXXXXXXXXXXXXXXXX</SignatureValue>
      <KeyInfo>
         <KeyValue>
            <RSAKeyValue>
               <Modulus>XXXXXXXXXXXXXXXXXXXXXXXX</Modulus>
               <Exponent>AQAB</Exponent>
            </RSAKeyValue>
         </KeyValue>
         <X509Data>
            <X509SubjectName>XXXXXXXXXXXXXXXXXXXXXXXX</X509SubjectName>
            <X509Certificate>XXXXXXXXXXXXXXXXXXXXXXXX</X509Certificate>
         </X509Data>
      </KeyInfo>
   </Signature>
</EsignResp>

In <UserX509Certificate> tag I get certificate details like Issued to,Issued By, Valid From . <UserX509Certificate>标记中,我获得证书详细信息,例如“ 颁发给”,“颁发者”,“有效自”

Is there any way to get these information using itextsharp(C#). 有什么方法可以使用itextsharp(C#)获取这些信息。

You don't need itestsharp for handling and parsing certificates. 您不需要itestsharp来处理和解析证书。 It's all about pdf and not required for xml. 都是关于pdf的,而不是xml所必需的。

You may convert Base64 string in to X509Certificate2 type using below code. 您可以使用以下代码将Base64字符串转换为X509Certificate2类型。

byte[] bytes = Convert.FromBase64String("MII<...>==");
var cert = new X509Certificate2(bytes);

Then cert variable above will have properties like 然后上面的cert变量将具有如下属性

cert.Issuer or cert.IssuerName
cert.Subject or cert.SubjectName

The content may be parsed by split(',').split('=') as per your requirement. 内容可以根据您的需要通过split(',')。split('=')进行解析。

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

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