我正在与第三方合作,后者通过字节数组向我发送证书。他们在XML文档中向我发送了3个字符串,其中包括x509Data,SignatureValue和DigestValue(用于调试)。
他们希望我使用x509Data证书中包含的证书公钥来验证SignatureValue是否有效。我正在填充证书,但是当我尝试验证时,它总是返回false。
这是我的代码:
byte[] SignatureValueBytes = Convert.FromBase64String(Signature.SignatureValue);
byte[] x509DataBytes = Convert.FromBase64String(Signature.x509Data);
byte[] DigestValueBytes = Convert.FromBase64String(Signature.DigestValue);
X509Certificate2 cert = new X509Certificate2(x509DataBytes);
using (RSA RSA = (RSA)cert.PublicKey.Key)
{
bool a = RSA.VerifyData(x509DataBytes, SignatureValueBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
Signature。*是来自XML文件的字符串。可以让一些灵魂指出我在哪里出问题了吗?
您所写的代码正在尝试验证SignatureValueBytes
是使用RSASSA-PKCS1-SHA256签名的x509DataBytes
签名。
假设您正确使用了RSASSA-PKCS1-SHA256,您可能想使用VerifyHash
和DigestValueBytes
而不是VerifyData
和x509DataBytes
。 (您也想用cert.GetRSAPublicKey()
代替cert.PublicKey.Key
)
byte[] SignatureValueBytes = Convert.FromBase64String(Signature.SignatureValue);
byte[] x509DataBytes = Convert.FromBase64String(Signature.x509Data);
byte[] DigestValueBytes = Convert.FromBase64String(Signature.DigestValue);
X509Certificate2 cert = new X509Certificate2(x509DataBytes);
using (RSA RSA = cert.GetRSAPublicKey())
{
bool a = RSA.VerifyHash(DigestValueBytes, SignatureValueBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}