Jump to content
Tuts 4 You

C# AES Encryption (Large Files)


Recommended Posts

Hi guys. Can someone explain why AES encryption with work large files lose or adding some bytes. :cc_confused:

Original size must be : 17,9 MB (18 819 479 bytes)

I get : 17,9 MB (18 819 488 bytes)

here code

public static void DataEncrypt(string fileIn, string fileOut)
FileStream FILE_In = new FileStream(fileIn, FileMode.Open, FileAccess.Read);
FileStream FILE_Out = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);byte[] TEST_KEY = new byte[]
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
byte[] TEST_IV = new byte[]
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};Rijndael AES = Rijndael.Create();AES.Key = TEST_KEY;
AES.IV = TEST_IV;CryptoStream cs = new CryptoStream(FILE_Out, AES.CreateEncryptor(), CryptoStreamMode.Write);int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead;
bytesRead = FILE_In.Read(buffer, 0, bufferLen);
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);//cs.FlushFinalBlock();

Thanks Advance.

Edited by N1tro
Link to comment

Thanks for answer.With use PaddingMode.None;

Unhandled Exception: System.Security.Cryptography.CryptographicException: Length of the data to encrypt is invalid.
at System.Security.Cryptography.RijndaelManagedTransform.EncryptData(Byte[] i nputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at TDCDec.DataEncrypt(String fileIn, String fileOut) in D:\TEST\AESTest\AESTest\Program.cs:line 51
at TDCDec.Main(String[] args) in D:\TEST\AESTest\AESTest\Program.cs:line 17

Line 17

DataEncrypt(FILE_In, FILE_Out);

Line 51


Sorry. Very bad know C# . On Pascal, everything looks much simpler but code slowly work :ermm:

Edited by N1tro
Link to comment

as kao said, it needs to be 16-byte aligned. See his link.

additionally, dont forget to close the out-file and free the buffers "buffer", "TEST_KEY" and "TEST_IV"...

  • Like 1
Link to comment

Delphi/Pascal shouldn't be slower to do AES encryption than .NET, i would look at whatever implementation you have for bottlenecks or factors that will slow things down.

Just in case you're confused as to what deepzero and Kao mean by 'aligned', the size of the data to be encrypted/decrypted MUST be a multiple of 16 (evenly divisible) otherwise you need to make the buffer larger to account for the 'missing' bytes and add this extra to the size to be encrypted:

Size: 49 = (16 * 3) + 1


Size: 64 = ((16 * 3) + 1) + 15 // [16 - 1 = 15 for alignment]



Edited by ghandi
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...