【发布时间】:2015-04-26 08:30:55
【问题描述】:
有谁知道如何使用 C# 从 x509 证书中获取数字签名值(实际上是在 x509Store 中,而不是从文件中验证)并在例如文本框中显示它。我知道 GetRawCertDataString() 返回整个 x509 证书的原始数据,其中包括最后一行的数字签名,但我找不到只返回数字签名的命令。
【问题讨论】:
标签: c# digital-signature x509
有谁知道如何使用 C# 从 x509 证书中获取数字签名值(实际上是在 x509Store 中,而不是从文件中验证)并在例如文本框中显示它。我知道 GetRawCertDataString() 返回整个 x509 证书的原始数据,其中包括最后一行的数字签名,但我找不到只返回数字签名的命令。
【问题讨论】:
标签: c# digital-signature x509
最好的方法是获取 ASN.1 解析器并提取数字签名,或者做一些 p/invoke 的事情。您将需要使用CryptDecodeObject 函数并将X509_CERT 作为lpszStructType 参数传递。该函数返回(在pvStructInfo 中)指向CERT_SIGNED_CONTENT_INFO 结构的指针。这个结构有Signature 字段,这是一个简单的BIT_BLOB 结构,在cbData 和pbData 字段中有字节数组(使用Marshal.Copy 将字节从非托管内存复制到托管字节数组)。
【讨论】:
public static string Sign(this X509Certificate2 x509, string message)
{
byte[] data = Encoding.UTF8.GetBytes(message);
byte[] signedData;
using (MD5 hasher = MD5.Create())
{
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509.PrivateKey;
signedData = rsa.SignData(data, hasher);
}
return Convert.ToBase64String(signedData);// +Environment.NewLine + Environment.NewLine;
//return ByteArrayToString(signedData); //Convert.ToBase64String(signedData);
}
【讨论】: