欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-31 11:55:16
解析的结构和代码: 复制代码 代码如下:x509certificatesubstring #region 文件描述#endregion #region 类修改记录 :...

解析的结构和代码:

复制代码 代码如下:

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;
        }