c# - 'System.Security.Cryptography.CryptographicException' : keyset does not exist 类型异常

标签 c# encryption cryptography rsa x509certificate

在此链接上执行的所有步骤 System.Security.Cryptography.CryptographicException: keyset does not exist

但对改正错误没有帮助。

static public string Build64(string idOrder, double Amount) {

        string StrForSign = KKBRequestStr.Replace("%ORDER%", idOrder).Replace("%AMOUNT%", string.Format("{0:f}", Amount).Replace(",", "."));
        X509Certificate2 KKbCert = new X509Certificate2(KKBpfxFile, KKBpfxPass);
        RSACryptoServiceProvider rsaCSP = (RSACryptoServiceProvider)KKbCert.PublicKey.Key;
        byte[] SignData = rsaCSP.SignData(ConvertStringToByteArray(StrForSign), "SHA1"); // keyset does not exist!!!!!!!!
        Array.Reverse(SignData);
        string ResultStr = "<document>" + StrForSign + "<merchant_sign type=\"RSA\">" + Convert.ToBase64String(SignData, Base64FormattingOptions.None) + "</merchant_sign></document>";
        return Convert.ToBase64String(ConvertStringToByteArray(ResultStr), Base64FormattingOptions.None);
    }

最佳答案

据我了解,您正在尝试使用只有公钥 key 的 RSA 来签署一些数据。

RSA 签名是文档验证的过程。您使用私钥 签署一份文件,然后使用公钥 来检查它是否真的属于您。换句话说,您不能使用公钥签署文档。

这就是您收到错误“键集不存在”的原因。您的 key 集不包含用于签署数据的私钥。
您需要从 KkbCert 中提取私钥并将其用于签名。

关于c# - 'System.Security.Cryptography.CryptographicException' : keyset does not exist 类型异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30723555/

相关文章:

python - 凯撒密码按关键字的 ASCII 值而不是数字进行移位

ios - 使用 Crypto++ 和 Bouncy CaSTLe 使用 Serpent 进行加密和解密?

encryption - 如何使用 DUKPT 加密在读卡器中生成密文?

C# 数组索引器

c# - 如何在 ICSharpCode.AvalonEdit.TextEditor 中更改文本颜色?

c# - 将行号添加到以 Release模式部署的 ASP.NET 网站的堆栈跟踪

python - Python 中的 AES-128 CBC 解密

c# - 算术运算上溢或下溢

Java RSA 密码学 : RSA modulus has a small prime factor

python - PyECC 示例 - Python 椭圆曲线密码学