Parse the substring structure of the digital signature of to get the digital signature time

  • 2020-05-30 20:59:44
  • OfStack

Parsing structure and code:


X509CertificateSubstring
#region  The file 

#endregion
#region  Class modification record  :  Every change 1 Group description 
#endregion
using System;
using System.Security.Cryptography.X509Certificates;
namespace DTLEntAdministration.Common
{
    /// <summary>
    ///  Digitally signed Substring structure 
    /// </summary>
    public class X509CertificateSubstring
    {
        //CN=Shenzhen DriveTheLife Software Technology Co.Ltd, OU=Digital ID Class 3 - Microsoft Software Validation v2, O=Shenzhen DriveTheLife Software Technology Co.Ltd, L=Shenzhen, S=Guangdong, C=CN
        #region  Private field 
        private string _CN = string.Empty;
        private string _OU = string.Empty;
        private string _O = string.Empty;
        private string _L = string.Empty;
        private string _S = string.Empty;
        private string _C = string.Empty;
        #endregion
        #region  Public read-only attribute 
        public string CN { get { return _CN; } }
        public string OU { get { return _OU; } }
        public string O { get { return _O; } }
        public string L { get { return _L; } }
        public string S { get { return _S; } }
        public string C { get { return _C; } }
        #endregion
        public X509CertificateSubstring() { }
        /// <summary>
        ///  will Substring The string is parsed into a structure 
        /// </summary>
        /// <param name="substring">Substring string </param>
        /// <returns>X509CertificateSubstring</returns>
        public static X509CertificateSubstring Parse(string substring)
        {
            X509CertificateSubstring xcs = new X509CertificateSubstring();
            string[] items = substring.Split(',');
            foreach (var item in items)
            {
                if (item.Trim().StartsWith("CN="))
                {
                    xcs._CN = item.Trim().Substring(3); continue;
                }
                if (item.Trim().StartsWith("OU="))
                {
                    xcs._OU = item.Trim().Substring(3); continue;
                }
                if (item.Trim().StartsWith("O="))
                {
                    xcs._O = item.Trim().Substring(2); continue;
                }
                if (item.Trim().StartsWith("L="))
                {
                    xcs._L = item.Trim().Substring(2); continue;
                }
                if (item.Trim().StartsWith("S="))
                {
                    xcs._S = item.Trim().Substring(2); continue;
                }
                if (item.Trim().StartsWith("C="))
                {
                    xcs._C = item.Trim().Substring(2); continue;
                }
            }
            return xcs;
        }
        /// <summary>
        ///  will Substring The string is parsed into a structure , And returns the presence or absence of a digital signature 
        /// </summary>
        /// <param name="pyFile"> The physical path to a digitally signed file </param>
        /// <param name="xcs">X509CertificateSubstring</param>
        /// <returns>bool</returns>
        public static bool TryParse(string pyFile, out X509CertificateSubstring xcs)
        {
            bool result = true;
            xcs = new X509CertificateSubstring();
            string SubstringCN = string.Empty;
            X509Certificate cert = null;
            try
            {
                cert = X509Certificate2.CreateFromSignedFile(pyFile);
            }
            catch (System.Security.Cryptography.CryptographicException ce)
            {
                // No digital signature , Ignore this exception .
                result = false;
            }
            catch (Exception ex)
            {
                result = false;
                throw ex;
            }
            if (cert != null)
            {
                xcs = X509CertificateSubstring.Parse(cert.Subject);
            }
            return result;
        }
    }
}

Sample code to call:


        /// <summary>
        ///  Gets the name of the digital signature 
        /// </summary>
        /// <param name="pyFile"> The physical path to a digitally signed file </param>
        /// <returns> A digital signature , Returns an empty string if there is no digital signature </returns>
        public static string GetX509CertificateSubstringCN(string pyFile)
        {
            string SubstringCN = string.Empty;
            X509CertificateSubstring xcs;
            if (X509CertificateSubstring.TryParse(pyFile, out xcs))
            {
                SubstringCN = xcs.CN;
            }
            return SubstringCN;
        }


Related articles: