【发布时间】:2019-05-29 12:20:27
【问题描述】:
在我的 WPF 应用程序 (.NET 4.6) 中,我需要使用 P12 证书文件使用 SHA-512 算法对字符串进行签名(包括在 Web 请求的标头中)。我这样做如下:
using (var rsa = myX509Certificate2.GetRSAPrivateKey()) {
myBytes = rsa.SignData(
Encoding.UTF8.GetBytes(stringToSign),
HashAlgorithmName.SHA512,
RSASignaturePadding.Pkcs1
);
}
这适用于测试和我的几乎所有客户,但奇怪的客户得到以下例外:
System.Security.Cryptography.CryptographicException: Invalid algorithm specified.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash, Int32 cbHash, ObjectHandleOnStack retSignature)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash)
at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, Int32 calgHash)
at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
at System.Security.Cryptography.RSA.SignData(Byte[] data, Int32 offset, Int32 count, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
at System.Security.Cryptography.RSA.SignData(Byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
最近发生在一位使用 Windows 7 SP1 的客户身上。
我正在努力通过现有的 SO 问题或一般的谷歌寻找答案。据我所知,这可能是由于在后台使用了不受支持的 Windows 加密服务提供商,但我不确定,因为我自己无法复制错误。
任何想法如何解决这个问题,无论是通过代码还是让受影响的客户安装任何特定的 Windows 更新?
【问题讨论】:
标签: c# .net windows x509certificate2 sha512