简体   繁体   中英

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

I am using service that returns the XML signature. now my task is to identify the signer name from response xml signature.

XML response signature format :

<?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 .

Is there any way to get these information using itextsharp(C#).

You don't need itestsharp for handling and parsing certificates. It's all about pdf and not required for xml.

You may convert Base64 string in to X509Certificate2 type using below code.

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

Then cert variable above will have properties like

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

The content may be parsed by split(',').split('=') as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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