title: C# Pass Encrypted Data from a file into a memory stream. Throwing exception
body: <p>Sort of a continuation from an earlier question, done a bunch of googling, but I don't think I'm on the right path here. Essentially what I'm doing is opening a file with a segment of code like so:</p>
  
  <pre><code>byte[] cipherText = File.ReadAllBytes(encryptedFile);
  </code></pre>
  
  <p>Then passing that byte array to a static bool method which calls the actual decryption method and returns true if no cryptographic exception is thrown. On my main form, I've got a button click which calls the static bool from an If statement, unlocking some additional interface items if it comes back true, and popping up an "invalid password" messagebox if it returns false. </p>
  
  <p>I stepped through the entire process, and its throwing a "Padding is invalid and cannot be removed." exception, which some searching has revealed that should happen when the wrong decryption key is supplied. I know I'm passing the same key, at least I'm typing it in, so I'm assuming the problem has to do with either the way the byte[] is passed, or with the streams themselves. Here's an example of the decryption method:</p>
  
  <pre><code>public static string Decrypt(string password, byte[] ciphertext)
      {
          byte[] key, iv;
          CreateKeyIV(password, out key, out iv);
          using (MemoryStream encrypted = new MemoryStream(ciphertext))
          using (CryptoStream dec = new CryptoStream(encrypted, _algorithm.CreateDecryptor(key, iv), CryptoStreamMode.Read))
          using (StreamReader reader = new StreamReader(dec))
          {
              try
              {
                  return reader.ReadToEnd();
              }
              catch (CryptographicException)
              {
                  throw new CryptographicException("Invalid password.");
              }
              finally
              {
                  encrypted.Close();
                  dec.Close();
                  reader.Close();
              }
          }
      }
  </code></pre>
  
  <p>Does anyone see what I'm missing?</p>