【发布时间】:2018-04-30 08:24:32
【问题描述】:
我正在尝试使用本地 HDD 上的证书将 rsa-sha512 签名应用于消息。 最终的 SignData 引发加密异常“指定的算法无效”。 但是,如果我在 RSACryptoServiceProvider 的新实例(通过导入原始 RSACryptoServiceProvider 的导出创建)上使用 SignData,则不会出现该异常。 原始版本引发异常是否有某种原因?由于“副本”明显不同,我更喜欢使用原件。
我使用的c#代码如下:
X509Certificate2 cert = new X509Certificate2("C:\\Certs\\" + certName + ".p12", certPassword, X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey;
UTF8Encoding ByteConverter = new UTF8Encoding();
byte[] unsignedBytes = ByteConverter.GetBytes(unsignedText);
byte[] signature;
//This raises an exception, "Invalid algorithm specified."
signature = csp.SignData(unsignedBytes, new SHA512CryptoServiceProvider());
//But if I make a copy of the RSACryptoServiceProvider, no exception is raised
RSACryptoServiceProvider cspCopy = new RSACryptoServiceProvider();
RSAParameters Key = csp.ExportParameters(true);
cspCopy.ImportParameters(Key);
signature = cspCopy.SignData(unsignedBytes, new SHA512CryptoServiceProvider());
【问题讨论】:
标签: c# .net cryptography digital-signature