signsha

error Keyset does not exist when I want to Use for sign data in c#


I want to use a encryption algorithm (custom encryption) but I have a problem when I want to sign my byte array in c# and use this sample of code.

using (var rsa = new RSACryptoServiceProvider())
            {
                
rsa.FromXmlString("<RSAKeyValue><Modulus>MIGfMA0GDAQAB</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");

                //// Write the message to a byte array using UTF8 as the encoding.
                

                try
                {
                    //// Sign the data, using SHA256 as the hashing algorithm 
                    signedBytes = rsa.SignData(originalData, new SHA256CryptoServiceProvider());

                    

                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                    return null;**your text**
                }
                finally
                {
                    //// Set the keycontainer to be cleared when rsa is garbage collected.
                    rsa.PersistKeyInCsp = false;
                }
            }

Now when the code reach to SIgnData I get this error error Keyset does not exist

Original data is a byte Array after using a hash function like sha256 for it.

how can i solve this problem? thanks


Solution

  • this is a clear error that you get from FromXmlString function of rsa algorithm, as I know you must use this complete format and structure of xml for your publik and private key,,

    the Correct format is

    <RSAKeyValue>
        <Modulus>xxxxxxx</Modulus>
        <Exponent>AQAB</Exponent>
        <P>xxxxxxx</P>
        <Q>xxxxx</Q>
        <DP>xxxxxx</DP>
        <DQ>xxxxxx</DQ>
        <InverseQ>xxxxx</InverseQ>
        <D>xxxxx</D>
    </RSAKeyValue>
    

    As you know X is replace by your public and private key,, Also you must Know its a Asymmetric method of encryption.

    and for conversion of your public and private ket to xml format you can use some site like

    https://raskeyconverter.azurewebsites.net/PemToXml?handler=ConvertXML

    best regard