当前位置:主页 > 软件编程 > .NET代码 >

解析数字签名的substring结构(获取数字签名时间)

时间:2020-10-20 15:12:58 | 栏目:.NET代码 | 点击:

解析的结构和代码:

复制代码 代码如下:

X509CertificateSubstring

#region 文件描述

#endregion

#region 类修改记录 : 每次修改一组描述

#endregion

using System;
using System.Security.Cryptography.X509Certificates;

namespace DTLEntAdministration.Common
{
    /// <summary>
    /// 数字签名的Substring结构
    /// </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 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 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>
        /// 将Substring字符串解析成结构体
        /// </summary>
        /// <param name="substring">Substring字符串</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>
        /// 将Substring字符串解析成结构体,并返回数字签名存在与否
        /// </summary>
        /// <param name="pyFile">读取数字签名的文件的物理路径</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)
            {
                //没有数字签名,忽略此异常.
                result = false;
            }
            catch (Exception ex)
            {
                result = false;
                throw ex;
            }
            if (cert != null)
            {
                xcs = X509CertificateSubstring.Parse(cert.Subject);
            }
            return result;
        }

    }
}

调用的示例代码:

复制代码 代码如下:

        /// <summary>
        /// 获取数字签名的名称
        /// </summary>
        /// <param name="pyFile">读取数字签名的文件的物理路径</param>
        /// <returns>数字签名,如果没有数字签名则返回空字符串</returns>
        public static string GetX509CertificateSubstringCN(string pyFile)
        {
            string SubstringCN = string.Empty;
            X509CertificateSubstring xcs;
            if (X509CertificateSubstring.TryParse(pyFile, out xcs))
            {
                SubstringCN = xcs.CN;
            }
            return SubstringCN;
        }

您可能感兴趣的文章:

相关文章