我已经尝试过了
X509Certificate2 _MiCertificado = new X509Certificate2(@"C:\Users\Lefczuk\Downloads\KitPruebas SW\CSD Pruebas\aad990814bp7_1210261233s.cer", "12345678a",X509KeyStorageFlags.MachineKeySet);
RSACryptoServiceProvider RSA1 = (RSACryptoServiceProvider)_MiCertificado.PrivateKey;
SHA1 hasher = new SHA1CryptoServiceProvider();
UTF8Encoding e = new UTF8Encoding(true);
byte[] bytesFirmados = RSA1.SignData(e.GetBytes(this.obtenerCadenaOriginal()),hasher);
Convert.ToBase64String(bytesFirmados);
但它抛出 NullReferenceException,RSA1 对象为 null。
有没有办法在不使用 .psk 或 .p12 文件或不编写 OpenSSL 命令的情况下解决此问题?
最佳答案
证书 (.cer) 文件(例如您正在加载的文件)不包含私钥,因此 _MiCertificado.PrivateKey
为 null(这就是为什么 RSA1
也为 null )。如果没有私钥,您就无法对数据进行签名,因此您必须加载包含私钥的文件(例如 .p12)。
关于c# - SHA1 与 RSA 在 C# 中使用私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21789843/