title: Rijndael Encryption | Padding is invalid and cannot be removed
body: <p>Im new to cryptography and im not quite sure what im doing wrong here,</p>
  
  <pre><code>public static byte[] EncryptData(byte[] data, string keystr)
  {
      if (keystr.Length &gt; 32)
          keystr = keystr.Substring(0, 32);
      else
          while (keystr.Length != 32)
              keystr += "0";
  
      byte[] iv = Encoding.UTF8.GetBytes(SALT);
      byte[] key = Encoding.UTF8.GetBytes(keystr);
  
      using (MemoryStream memoryStream = new MemoryStream())
      {
          using (RijndaelManaged rijndaelManaged = new RijndaelManaged { Key = key, IV = iv, Padding = PaddingMode.PKCS7, Mode = CipherMode.CBC })
          {
              using (CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateEncryptor(key, iv), CryptoStreamMode.Write))
              {
                  cryptoStream.Write(data, 0, data.Length);
              }
          }
          return memoryStream.ToArray();
      }
  }
  
  public static byte[] DecryptData(byte[] data, string keystr)
  {
      if (keystr.Length &gt; 32)
          keystr = keystr.Substring(0, 32);
      else
          while (keystr.Length != 32)
              keystr += "0";
  
      byte[] iv = Encoding.UTF8.GetBytes(SALT);
      byte[] key = Encoding.UTF8.GetBytes(keystr.ToUpper());
  
      using (MemoryStream memoryStream = new MemoryStream())
      {
          using (RijndaelManaged rijndaelManaged = new RijndaelManaged { Key = key, IV = iv, Padding = PaddingMode.PKCS7, Mode = CipherMode.CBC })
          {
              using (CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateDecryptor(key, iv), CryptoStreamMode.Write))
              {
                  cryptoStream.Write(data, 0, data.Length);
              }
          }
          return memoryStream.ToArray();
      }
  }
  </code></pre>
  
  <p>As you can see im passing in a byte array and a password. i make sure the password is always 32 chars.</p>
  
  <p>Im getting <code>Padding is invalid and cannot be removed.</code> when i decrypt data.</p>
  
  <p>The key and salt is always the same.</p>