C# RSA Encrypt / Decrypt Problem

Posted by Brendon Randall on Stack Overflow See other posts from Stack Overflow or by Brendon Randall
Published on 2010-03-19T08:28:11Z Indexed on 2010/03/19 8:31 UTC
Read the original article Hit count: 399

Filed under:
|
|
|

Hi All,

Im having a problem with C# encrypting and decrypting using RSA. I have developed a web service that will be sent sensitive financial information and transactions. What I would like to be able to do is on the client side, Encrypt the certain fields using the clients RSA Private key, once it has reached my service it will decrypt with the clients public key.

At the moment I keep getting a "The data to be decrypted exceeds the maximum for this modulus of 128 bytes." exception. I have not dealt much with C# RSA cryptography so any help would be greatly appreciated.

This is the method i am using to generate the keys

private void buttonGenerate_Click(object sender, EventArgs e)
    {
        string secretKey = RandomString(12, true);

        CspParameters param = new CspParameters();
        param.Flags = CspProviderFlags.UseMachineKeyStore;

        SecureString secureString = new SecureString();
        byte[] stringBytes = Encoding.ASCII.GetBytes(secretKey);
        for (int i = 0; i < stringBytes.Length; i++)
        {
            secureString.AppendChar((char)stringBytes[i]);
        }
        secureString.MakeReadOnly();
        param.KeyPassword = secureString;

        RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(param);
        rsaProvider = (RSACryptoServiceProvider)RSACryptoServiceProvider.Create();
        rsaProvider.KeySize = 1024;


        string publicKey = rsaProvider.ToXmlString(false);
        string privateKey = rsaProvider.ToXmlString(true);

        Repository.RSA_XML_PRIVATE_KEY = privateKey;
        Repository.RSA_XML_PUBLIC_KEY = publicKey;

        textBoxRsaPrivate.Text = Repository.RSA_XML_PRIVATE_KEY;
        textBoxRsaPublic.Text = Repository.RSA_XML_PUBLIC_KEY;

        MessageBox.Show("Please note, when generating keys you must sign on to the gateway\n" +
            " to exhange keys otherwise transactions will fail", "Key Exchange", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

Once i have generated the keys, i send the public key to the web service which stores it as an XML file.

Now i decided to test this so here is my method to encrypt a string

public static string RsaEncrypt(string dataToEncrypt)
    {
        string rsaPrivate = RSA_XML_PRIVATE_KEY;
        CspParameters csp = new CspParameters();
        csp.Flags = CspProviderFlags.UseMachineKeyStore;

        RSACryptoServiceProvider provider = new RSACryptoServiceProvider(csp);

        provider.FromXmlString(rsaPrivate);

        ASCIIEncoding enc = new ASCIIEncoding();
        int numOfChars = enc.GetByteCount(dataToEncrypt);
        byte[] tempArray = enc.GetBytes(dataToEncrypt);
        byte[] result = provider.Encrypt(tempArray, true);
        string resultString = Convert.ToBase64String(result);
        Console.WriteLine("Encrypted : " + resultString);
        return resultString;
    }

I do get what seems to be an encrypted value. In the test crypto web method that i created, i then take this encrypted data, try and decrypt the data using the clients public key and send this back in the clear. But this is where the exception is thrown. Here is my method responsible for this.

public string DecryptRSA(string data, string merchantId)
    {
        string clearData = null;
        try
        {
            CspParameters param = new CspParameters();
            param.Flags = CspProviderFlags.UseMachineKeyStore;
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(param);

            string merchantRsaPublic = GetXmlRsaKey(merchantId);
            rsaProvider.FromXmlString(merchantRsaPublic);
            byte[] asciiString = Encoding.ASCII.GetBytes(data);

            byte[] decryptedData = rsaProvider.Decrypt(asciiString, false);

            clearData = Convert.ToString(decryptedData);
        }
        catch (CryptographicException ex)
        {
            Log.Error("A cryptographic error occured trying to decrypt a value for " + merchantId, ex);

        }
        return clearData;

If anyone could help me that would be awesome, as i have said i have not done much with C# RSA encryption/decryption.

Thanks in advance

© Stack Overflow or respective owner

Related posts about c#

Related posts about rsa