title: C#: AES error: Padding is invalid and cannot be removed. Same key and everything, help
body: <p>I'm quite new to C# so please be patient with me. I know this question was asked a lot if times, but I couldn't find an answer to my problem.</p>
  
  <p>I'm saving some data and before writing it to a file I convert it to binary and store it in array, which I encrypt and then write to file. I encrypt data in chunks (32 bytes). In the same way I read data in chunks of 32 bytes and then decrypt that data and then this should repeat till the end of file. But when it comes to decryption the following error is thrown:</p>
  
  <blockquote>
    <p><strong>Padding is invalid and cannot be removed.</strong></p>
  </blockquote>
  
  <p>I use the same key and iv (hardcoded just until I get it working)</p>
  
  <p>Here is my encryption code, which works without problems:</p>
  
  <pre><code>        //result
          byte[] data = new byte[32];
  
          //setup encryption (AES)
          SymmetricAlgorithm aes = Aes.Create();
          byte[] key = { 145, 12, 32, 245, 98, 132, 98, 214, 6, 77, 131, 44, 221, 3, 9,50};
          byte[] iv = { 15, 122, 132, 5, 93, 198, 44, 31, 9, 39, 241, 49, 250, 188, 80, 7 };
          ICryptoTransform encryptor = aes.CreateEncryptor(key, iv);
  
          FileStream fStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, 1024, false);
  
          //prepare data to write (byte array 'data') ...
  
          //encrypt
                 MemoryStream m = new MemoryStream();
                 using (Stream c = new CryptoStream(m, encryptor, CryptoStreamMode.Write))
                     c.Write(data, 0, data.Length);
                 data = m.ToArray();
                 fStream.Write(data, 0, data.Length);
  </code></pre>
  
  <p>And here is my decryption code:</p>
  
  <pre><code>FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false);
  
              //setup encryption (AES)
              SymmetricAlgorithm aes = Aes.Create();
              byte[] key = { 145, 12, 32, 245, 98, 132, 98, 214, 6, 77, 131, 44, 221, 3, 9, 50 };
              byte[] iv = { 15, 122, 132, 5, 93, 198, 44, 31, 9, 39, 241, 49, 250, 188, 80, 7 };
              ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);
  
              //result
              byte[] data = new byte[32];
  
              //loop for reading the whole file ...
              int len = fStream.Read(data, 0, 32);
  
              //decrypt
                  MemoryStream m = new MemoryStream();
                  using (Stream c = new CryptoStream(m, decryptor, CryptoStreamMode.Write))
                      c.Write(data, 0, data.Length); //The exception is thrown in this line                  
                  data = m.ToArray();
  
                  //using the decrypted data and then looping back to reading and decrypting...
  </code></pre>
  
  <p>I tried all I could think of (which is not much because I'm very new to cryptography), I searched everywhere and I couldn't find a solution to my problem. I also helped myself with the book <em>C# in a Nutshell</em> .</p>
  
  <p>If anyone has ideas on why this could happen I'll be really thankful because I have no ideas.</p>
  
  <p>Thank you for your time and answers.</p>
  
  <p>EDIT:
  It seems that the size of the encrypted data is 48 bytes (12 bytes more than the original). Why is that so? I thought that it only adds bytes if they are not a multiple of the block size (16 bytes, my data is 32 bytes). Is data always larger, and with constant increase (I need to know that in order to properly read and decrypt).</p>
  
  <p>Note: I can't directly use other streams because I need to have control over the output format and I believe it is also safer and faster to encrypt in memory.</p>