解析数字签名的substring结构(获取数字签名时间)
本文导语: 解析的结构和代码: 代码如下:X509CertificateSubstring #region 文件描述#endregion #region 类修改记录 : 每次修改一组描述 #endregion using System;using System.Security.Cryptography.X509Certificates; namespace DTLEntAdministration.Common{ /// /// 数字签名...
解析的结构和代码:
X509CertificateSubstring
#region 文件描述
#endregion
#region 类修改记录 : 每次修改一组描述
#endregion
using System;
using System.Security.Cryptography.X509Certificates;
namespace DTLEntAdministration.Common
{
///
/// 数字签名的Substring结构
///
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() { }
///
/// 将Substring字符串解析成结构体
///
/// Substring字符串
/// X509CertificateSubstring
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;
}
///
/// 将Substring字符串解析成结构体,并返回数字签名存在与否
///
/// 读取数字签名的文件的物理路径
/// X509CertificateSubstring
/// bool
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;
}
}
}
调用的示例代码:
///
/// 获取数字签名的名称
///
/// 读取数字签名的文件的物理路径
/// 数字签名,如果没有数字签名则返回空字符串
public static string GetX509CertificateSubstringCN(string pyFile)
{
string SubstringCN = string.Empty;
X509CertificateSubstring xcs;
if (X509CertificateSubstring.TryParse(pyFile, out xcs))
{
SubstringCN = xcs.CN;
}
return SubstringCN;
}
您可能感兴趣的文章:
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。