title: Padding is invalid and cannot be removed in C#
body: <p>I'm making a program with encryting and decrypting feature, but I have encountered <strong>Padding is invalid and cannot be removed</strong> problem.</p>
  
  <p>Here are my codes:</p>
  
  <p><em>Encrypt</em></p>
  
  <pre><code>public static string EncryptString(string plainText, string passPhrase)
          {
              byte[] initVectorBytes = Encoding.UTF8.GetBytes("pemgail9uzpgzl88");
              byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
              PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
              byte[] keyBytes = password.GetBytes(256 / 8);
              RijndaelManaged symmetricKey = new RijndaelManaged();
              symmetricKey.Mode = CipherMode.CBC;
              ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
              MemoryStream memoryStream = new MemoryStream();
              CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
              cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
              cryptoStream.FlushFinalBlock();
              byte[] cipherTextBytes = memoryStream.ToArray();
              memoryStream.Close();
              cryptoStream.Close();
              return Convert.ToBase64String(cipherTextBytes);
          }
  </code></pre>
  
  <p><em>Decrypt</em></p>
  
  <pre><code>public static string DecryptString(string cipherText, string passPhrase)
      {
          byte[] initVectorBytes = Encoding.UTF8.GetBytes("pemgail9uzpgzl88");
          byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
          PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
          byte[] keyBytes = password.GetBytes(256 / 8);
          RijndaelManaged symmetricKey = new RijndaelManaged();
          symmetricKey.Mode = CipherMode.CBC;
          ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
          MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
          CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
          byte[] plainTextBytes = new byte[cipherTextBytes.Length];
          int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
          memoryStream.Close();
          cryptoStream.Close();
          return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
      }
  </code></pre>
  
  <p>The process that is wrong is decrytping, at the line <strong>cryptoStream.Close</strong>.</p>