An exception of type 'System.Security.Cryptography.CryptographicException': keyset does not exist

2.8k views Asked by At

All the steps made at this link System.Security.Cryptography.CryptographicException: keyset does not exist

But it did not help to correct the error.

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);
    }
1

There are 1 answers

1
Yeldar Kurmangaliyev On BEST ANSWER

As I understand, you are trying to sign some data using RSA having only a public key.

RSA signing is a process of document verification. You sign a document using private key and then use public key to check if it is really yours. In other words, you cannot sign document with public key.

That's why you get error "keyset does not exist". Your keyset doesn't contain a private key for signing data.
You need to extract the private key from your KkbCert and use it for signing.