1

I'm trying to build Bob and Alice asymmetric encryption and decryption implementation using RSACryptoServiceProvider

for that I have

  1. console app Bob (can consider as the sender)
  2. console app Alice (can consider as the receiver)

console app Bob can encrypt using its public key and then console app Alice can decrypt this using its private key

so this is Bob Console app

    class Program
    {
        static void Main(string[] args)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  

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

            EncryptText(publicKey, "Hello from C# Corner", "encryptedData.dat");
        }

        public static void EncryptText(string publicKey, string text, string fileName)
        {
            UnicodeEncoding byteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = byteConverter.GetBytes(text);
            byte[] encryptedData;
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(publicKey);
                encryptedData = rsa.Encrypt(dataToEncrypt, false);
            } 
            File.WriteAllBytes(fileName, encryptedData);  
            Console.WriteLine("Data has been encrypted");
        }
    }

this is Alice Console app

class Program
{
    static void Main(string[] args)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
        string publicKey = rsa.ToXmlString(false);  
        string privateKey = rsa.ToXmlString(true);     
     
        Console.WriteLine("Decrypted message: {0}", DecryptData(privateKey, "encryptedData.dat"));
    }

    public static string DecryptData(string privateKey, string fileName)
    {

        byte[] dataToDecrypt = File.ReadAllBytes(fileName);
        byte[] decryptedData;
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            rsa.FromXmlString(privateKey);
            decryptedData = rsa.Decrypt(dataToDecrypt, false);
        }
        UnicodeEncoding byteConverter = new UnicodeEncoding();
        return byteConverter.GetString(decryptedData);
    }
}

when It's decrypting I'm getting the error as

System.Security.Cryptography.CryptographicException: 'The parameter is incorrect.

could you please advise on this :)

Kelum
  • 1,745
  • 5
  • 24
  • 45

1 Answers1

1

The reason here is that you're constructing a public/private key pair in both applications.

Essentially you're trying to encrypt something with Bob's public key, and trying to decrypt that with Alice's private key. This won't work.

You need to construct 1 pair, and then use the public key from that pair for the encryption, and the private key from the same pair for the decryption.

This works:

void Main()
{
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    string publicKey = rsa.ToXmlString(false);
    string privateKey = rsa.ToXmlString(true);
    
    EncryptText(publicKey, "Test", @"d:\temp\test.dat");
    Console.WriteLine(DecryptData(privateKey, @"d:\temp\test.dat"));
}

but if I create a new pair before calling decrypt, I get this error:

WindowsCryptographicException
The parameter is incorrect.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825