Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im creating an instant messaging service and whenever i want to decrypt data (except for the first message the client sends for some reason) it gives me the following error "Length of the data to decrypt is invalid.". I used a lot of the code from the microsoft website and in my dummy program it works perfect but for some reason my second message does not work properly

public byte[] RijEncryptString(string plainText, byte[] key, byte[] iv)
        {
            byte[] encrypted;

            using (Rijndael rijAlg = Rijndael.Create())
            {
                rijAlg.Key = key;
                rijAlg.IV = iv;

                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //write data to the stream
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            //returns the encrypted bytes from the memory stream
            return encrypted;
        }

        public string RijDecryptString(byte[] cipherText, byte[] key, byte[] iv)
        {
            string plainText = string.Empty;

            using (Rijndael rijAlg = Rijndael.Create())
            {
                rijAlg.Key = key;
                rijAlg.IV = iv;

                //creates a decryptor to preform the stream transform
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            //read the decrypted bytes and put them in a string
                            plainText = srDecrypt.ReadToEnd();
                        }
                    }
                }
                //return the decrypted text
                return plainText;
            }
        }
Posted
Comments
I think you need to use a PaddingMode like PaddingMode.PKCS7
Member 11382537 18-Jan-15 0:19am    
i tried making it paddingMode.None and PaddingMode.PKCS7. None worked
Okay then do one thing. Search this exception in Google. There are many solutions as I can see. Please try one by one. Be patient and try.
Member 11382537 18-Jan-15 0:30am    
I have tried and none work. But i will keep looking, thanks
Yeah, do that. If you still fail, then update the question and add all the solutions you have tried. That would help us.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900