【发布时间】:2014-12-23 21:13:57
【问题描述】:
我花了 100 个小时研究这个主题,而其他编写原始项目的高级程序员也无法使其工作。我有一个带有 SignatureValue、Certificate (X509Certificate2) 和 Digest Value 参数的 xml。通过将连接字段(等于摘要值)转换为哈希(SHA1),然后通过私钥加密来生成在同一 xml 中声明的创建和给定签名值。私钥从证书中取出以保护隐私,我只有公钥。现在,无论我如何编码,我总是得到一个错误的值(如在 VerifyHash/verifyHashResult 为假)。这是我正在使用的代码:
// 需要你的帮助。
static void VerifyHash(string sigVal , string digestVal, System.Security.Cryptography.X509Certificates.X509Certificate2 cert)
{
sigValInBytes = Convert.FromBase64String(sigVal);
try
{
using (RSACryptoServiceProvider rsaProviderDecrypt = (RSACryptoServiceProvider)cert.PublicKey.Key)
{
// Line below always return value of FALSE no matter how I code it. Here I want to verify the hashed freshly calculated digest value that is now hashed with the signature value
rsaProviderDecrypt.Decrypt(sigValInBytes, false);
rsaProviderDecrypt.Dispose();
}
}
}
// At the main program I get the certificate from the xml given and call the method above:
main
{
// Code below gets the certificate details from a given xml, details of each variable confirmed to be accurate.
char[] Base64_x509ByteArray;
Base64_x509ByteArray = t.DigitalSignatures.First().X509Data.ToCharArray();
byte[] x509ByteArray;
x509ByteArray = Convert.FromBase64CharArray(Base64_x509ByteArray, 0, Base64_x509ByteArray.Length);
// Here am creating the certificate from the gathered data/certificate:
System.Security.Cryptography.X509Certificates.X509Certificate2 cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(x509ByteArray);
VerifyHash(t.DigitalSignatures.FirstOrDefault().SignatureValue.Trim(), concatenatedFieldValues, cert);
}
【问题讨论】:
-
ByteConverter类在做什么?为什么签名和摘要是string? -
有什么理由要使用
X509Certificate2吗?可以给你另一个建议吗? -
对于问题 #1,我继续进行了一些更改并删除了字节转换,因为我不需要刚刚发布的新代码。我认为我更近了一步,并且在使用 rsaProviderDecrypt.Decrypt(sigValInBytes, false) 时做正确的事情,尤其是在使用 FromBase64String 将 sigVal 转换为字节数组之后,因为这使得字节数组的大小不大于 256。但是收到一个新错误“密钥不存在”
-
对于问题 #2:我必须使用 X509Certificate,因为这是供应商用来加密数字签名的(我这里别无选择)。
-
知道为什么我会收到新错误“找不到密钥”吗?
标签: c# cryptography digital-signature x509certificate2 xml-signature