Question : RSA descryption give bad Data exception

Hi All,

I am wondering if there is anyone here used RSA public key, private key encryption, decryption, i am using it on my application, but when encrypted keys generated and wanted to use them to decrypt ,, i am recieving "Bad data" exception all the time?

Anyone have an idea why its generating this?

Code for encryption, decryption

        public static string encryptData(string phrase)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
            TextReader readerPublic = File.OpenText("PublicKey.xml");
            rsa.FromXmlString(readerPublic.ReadToEnd());
            readerPublic.Close();

            byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(phrase);
            byte[] cipherbytes = rsa.Encrypt(plainBytes, false);
            string encryptedData = Convert.ToBase64String(cipherbytes);
            return encryptedData;
        }

        public static string decryptData(string encryptedData)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
            TextReader readerPublic = File.OpenText("PrivateKey.xml");
            rsa.FromXmlString(readerPublic.ReadToEnd());
            readerPublic.Close();

            byte[] getData = Convert.FromBase64String(encryptedData);
            byte[] plain = rsa.Decrypt(getData, false);
            string decryptedData = System.Text.Encoding.UTF8.GetString(plain);
            return decryptedData;

        }

Thanks

Answer : RSA descryption give bad Data exception

The easiest way to ignore case is to always convert to uppercase before encrypting.  Otherwise, I am not sure if there is an option for case-sensitivity.

Bob
Random Solutions  
 
programming4us programming4us