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:
byte[] cipherText = File.ReadAllBytes(encryptedFile);
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.
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:
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();
}
}
}
Does anyone see what I'm missing?