我的应用程序将采用一组文件并对其进行签名.(我不是要尝试签署程序集.)有一个.p12文件,我从中获取私钥.
这是我试图使用的代码,但我得到了一个System.Security.Cryptography.CryptographicException "Invalid algorithm specified."
.
X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password");
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey;
string id = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(File.ReadAllBytes(filePath), id);
Run Code Online (Sandbox Code Playgroud)
根据这个答案,它无法完成(RSACryptoServiceProvider
不支持SHA-256),但我希望可以使用不同的库,如Bouncy Castle.
我是新手,我发现Bouncy Castle非常混乱.我正在将一个Java应用程序移植到C#,我必须使用相同类型的加密来签名文件,所以我坚持使用RSA + SHA256.
我怎么能用Bouncy Castle,OpenSSL.NET,Security.Cryptography或其他我没有听说过的第三方库来做到这一点?我假设,如果它可以在Java中完成,那么它可以在C#中完成.
更新:
这是我从poupou的anwser链接中得到的
X509Certificate2 cert = new X509Certificate2(KeyStoreFile, password");
RSACryptoServiceProvider rsacsp = (RSACryptoServiceProvider)cert.PrivateKey;
CspParameters cspParam = new CspParameters();
cspParam.KeyContainerName = rsacsp.CspKeyContainerInfo.KeyContainerName;
cspParam.KeyNumber = rsacsp.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2;
RSACryptoServiceProvider aescsp = new RSACryptoServiceProvider(cspParam);
aescsp.PersistKeyInCsp = false;
byte[] signed = aescsp.SignData(File.ReadAllBytes(file), "SHA256");
bool isValid …
Run Code Online (Sandbox Code Playgroud)