Rijndael class to encrypt and then decrypt data raise "Rijndael class to encrypt and then decrypt data" error
<p>I use the function <code>EncryptStringToBytes</code> to encrypt plain text to array of bytes and
finally convert array of bytes to string and return it then.</p>

<p>I use another function to Decrypt Encrypted text to plaint text.</p>

<p>I try to scramble a text with RC2 but it raises this error:</p>

<blockquote>
  <p>Rijndael class to encrypt and then decrypt data</p>
</blockquote>

<p>Here's the code:</p>

<pre><code>using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace RC2CryptoServiceProvider_Examples
{
    class MyMainClass
    {
        static string EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length &lt;= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length &lt;= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length &lt;= 0)
                throw new ArgumentNullException("Key");
            byte[] encrypted;
            // Create an Rijndael object
            // with the specified key and IV.
            using (Rijndael rijAlg = Rijndael.Create())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

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


            **// Return the encrypted bytes from the memory stream.
            return System.Text.Encoding.UTF8.GetString(encrypted);**

        }
        static string DecryptStringFromBytes(string Codedtext, byte[] Key, byte[] IV)
        {
            byte[] cipherText = System.Text.Encoding.UTF8.GetBytes(Codedtext);
            // Check arguments.
            if (cipherText == null || cipherText.Length &lt;= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length &lt;= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length &lt;= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an Rijndael object
            // with the specified key and IV.
            using (Rijndael rijAlg = Rijndael.Create())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                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 from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }

        public static void Main()
        {

            try
            {
                System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();

                string original = "Here is some data to encrypt!";

                // Create a new instance of the Rijndael
                // class.  This generates a new key and initialization 
                // vector (IV).
                using (Rijndael myRijndael = Rijndael.Create())
                {
                    // Encrypt the string to an array of bytes.
                    string encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

                    // Decrypt the bytes to a string.
                    string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                    //Display the original data and the decrypted data.
                    Console.WriteLine("Original:   {0}", original);
                    Console.WriteLine("Round Trip: {0}", roundtrip);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
            Console.ReadLine();
        }
    }
}
</code></pre>
