Hi, everybody
I am working on AES encryption app. I need to split the file into blocks, then encrypt each block individually. What I had so far is below:

    byte[] sourceBuffer = File.ReadAllBytes(inputFile);
    byte[] destBuffer = new byte[sourceBuffer.Length];
    byte[] toEncrypt = new byte[16];

            // Encrypt each block.
            for (int offset = 0; offset < sourceBuffer.Length; offset += 16)
            {

                Array.Copy(sourceBuffer, offset, toEncrypt, 0, 16); // copying from the source into 16 byte array size

                Array.Copy(EncryptByte(toEncrypt), 0, destBuffer, offset, 16); 

            }
            System.IO.File.WriteAllBytes(outputFile, destBuffer);

            /********************************************************************************************************/

                   public static byte[] EncryptByte(byte[] input)
        {
            RijndaelManaged aes = new RijndaelManaged();
            MemoryStream ms = new MemoryStream();
            aes.IV = Iv;
            aes.Key = Key;
            aes.Padding = PaddingMode.Zeros;

            CryptoStream cs = new CryptoStream(ms,aes.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(input, 0, input.Length);
            cs.Close();
            return ms.ToArray();
        }

Instead of ReadAllBytes try the BinaryReader.Read Method which has an overload to fill a buffer on each read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.