Hi friends.
The headline kinda says what needs to be said. it is all working fine exept that the decrypted file is containing rubbish. and wierdly enough it is smaller than the plaintext and cipher?
Thanks for your help :)
EDIT: I got some more information... now it has the correct length. now the decrypted file is 1 block short of the encrypted file, and it says: "padding is invalid and cannot be removed". I guess that this is maybe caused since the decryption fails and the padding then can't be recognized? I don't know.
Unfortunatly all the other decrypted blocks are wrong (rubbish)..
very frustrated. :(
private void encryptFile(byte[] key, byte[] iv, int keySize, CipherMode CM, FileStream fileInput, FileStream fileOutput)
{
try
{
RijndaelManaged AES = new RijndaelManaged();
AES.IV = iv;
AES.Mode = CM;
AES.Key = key;
AES.KeySize = keySize;
int blockSize = 16;
byte[] byteArrayInput = new byte[blockSize];
long numBlocks = fileInput.Length / blockSize;
long numBlocksDone = 0;
ICryptoTransform aesEncrypt = AES.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(fileOutput, aesEncrypt, CryptoStreamMode.Write);
for (long l = 0; l < numBlocks; l++)
{
fileInput.Read(byteArrayInput, 0, byteArrayInput.Length);
cryptoStream.Write(byteArrayInput, 0, byteArrayInput.Length);
numBlocksDone++;
reportWhileEncrypting(numBlocksDone, numBlocks);
}
if (fileInput.Length % blockSize > 0L)
{
byteArrayInput = new byte[fileInput.Length % blockSize];
fileInput.Read(byteArrayInput, 0, byteArrayInput.Length);
cryptoStream.Write(byteArrayInput, 0, byteArrayInput.Length);
}
cryptoStream.Close();
fileInput.Close();
fileOutput.Close();
}
catch (Exception e)
{
statusHandler.addStatus("AES encryption...", statusReport.status.Error, "Error: " + e.Message);
}
}
private void decryptFile(byte[] key, byte[] iv, int keySize, CipherMode CM, FileStream fileInput, FileStream fileOutput)
{
try
{
RijndaelManaged AES = new RijndaelManaged();
AES.IV = iv;
AES.Mode = CM;
AES.Key = key;
AES.KeySize = keySize;
int blockSize = 16;
byte[] byteArrayInput = new byte[blockSize];
long numBlocks = fileInput.Length / blockSize;
long numBlocksDone = 0;
ICryptoTransform aesDecrypt = AES.CreateDecryptor();
CryptoStream cryptoStream = new CryptoStream(fileInput, aesDecrypt, CryptoStreamMode.Read);
for (long l = 0; l < numBlocks; l++)
{
cryptoStream.Read(byteArrayInput, 0, byteArrayInput.Length);
fileOutput.Write(byteArrayInput, 0, byteArrayInput.Length);
numBlocksDone++;
reportWhileEncrypting(numBlocksDone, numBlocks);
}
cryptoStream.Close();
fileInput.Close();
fileOutput.Close();
}
catch (Exception e)
{
statusHandler.addStatus("AES Decryption...", statusReport.status.Error, "Error: " + e.Message);
}
}

2 answers
Just to show that it definitely is possible to use RijndaelManaged to encrypt/decrypt one block at a time, I've adjusted the MS Support article code used in previous threads to allow for this. It works fine on the (small) files I've tested it on.
answered one year ago by:
17279
66
I got it working finally... Thx for all your help :)
2309
I've always hated managed aes implementation. rijndael doesn't use an iv. that base class is pretty much a des/triple des base but doesn't make sense with others.
66
Why you hate it? :) and it's not the algorithms that uses the IV, it's the cipherMode, which just ensures different cipher for same plaintext, and it has nothing to do with the cipher algorithm itself.
2309
aes doesn't use a cipher mode or iv. it has it's own substitution box and works way different than des/3des (which uses the iv to xor the initial block, and uses the cipher mode to determine whether it xor's the previous block, or whatever). both these are useless to aes, and therefore the algorithm base class is a very bad interface for all symmetric encryption algorithms.
I don't know whether it's the only problem but there certainly is a problem with the way you're decrypting the final block.
Any trailing zero bytes need to be trimmed off before writing it to the decrypted file as these will reflect padding added during the encryption process due to the plaintext file size not being an exact multiple of the block size.
I've adjusted your decryptFile method below to allow for this:
answered one year ago by:
17279
66
DIdn't change anything... and i guess that the paddig would be added beforehand the encryption (since it's neede to encrypt) and then the zero bytes would be ebcrypted, hence we can't really remove them before decrypting? anyway, all the decrypted blocks are rubbish unfortunatly :(
17279
The extra code is removing the extra bytes after decryption so that the decrypted text file length will be the same as the original file length. I don't know what else could be wrong unless there's some mismatch in the key, IV or mode being passed to the encrypt and decrypt methods but see my additional answer below.