Task is to encrypt part of the file using aes encryption algorithm. I did

public static byte[] AES_(int len, byte[] passwordBytes, byte[] saltBytes, FileStream fSin)

    byte[] encryptedBytes = null;
    byte[] encryptedBytesAndIV = null;

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        using (AesCryptoServiceProvider AES = new AesCryptoServiceProvider())
            AES.KeySize = 256;
            AES.BlockSize = 128;

            var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = generateIV();

            AES.Mode = CipherMode.CBC;

            using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                byte[] buffer = new byte[1048576];
                int read,read2=0;

                while (read2 < len)
                    read = fSin.Read(buffer, 0, buffer.Length);
                    cs.Write(buffer, 0, read);
                    read2 += read;

            encryptedBytes = ms.ToArray();
            encryptedBytesAndIV = new byte[encryptedBytes.Length + AES.IV.Length];
            AES.IV.CopyTo(encryptedBytesAndIV, 0);
            encryptedBytes.CopyTo(encryptedBytesAndIV, IV_LENGTH);

    return encryptedBytesAndIV;
private static byte[] generateIV()
    using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
        byte[] nonce = new byte[IV_LENGTH];
        return nonce;

call function

  FileStream fsIn = new FileStream(path, FileMode.Open);
len = Convert.ToInt32(fsIn.Length) / 3;
 var resultb = AES_(len, passwordBytes, saltBytes, fsIn);

    fsIn.Seek(0, SeekOrigin.Begin);
    fsIn.Write(resultb, 0, resultb.Length);

    fsIn.Seek(fsIn.Length, SeekOrigin.Begin);
    fsIn.Write(saltBytes, 0, saltBytes.Length);
using (var writer = new BinaryWriter(fsIn))
    writer.Seek(Convert.ToInt32(fsIn.Length), SeekOrigin.Begin);


The problem is that when a large file consumes a lot of memory and the computer freezes.Is it possible to alter this code so that the memory consumption of the PC was the minimum ?

As I understand it, that is the memory write bytes to the MemoryStream, but how different ? I need to overwrite the bytes in the same file if the same record byte immediately into a new file, it will have to write all the remaining bytes, and it is a waste of time and also memory

  • \$\begingroup\$ a large file consumes a lot of memory a lot of data requries a lot of memory and since you are not just encrpting the file with a stream but also return the encrypted version from the method you need to load it completely into memory. \$\endgroup\$ – t3chb0t Jul 19 '17 at 11:11
  • \$\begingroup\$ @t3chb0t it turns out that nothing can be done ? \$\endgroup\$ – Lolidze Jul 19 '17 at 11:25
  • 1
    \$\begingroup\$ I believe that it could be possible but you don't provide enough code. Currently you read a part from a file, encrypt it and read into memory... then I guess you use this buffer to write it into a new file. I'm pretty sure this could be done fluently so that you read from a file, then encrypt a few bytes then write the rest into the new file without storing too much data in memory. But the part where you read, encrypt, write is missing. It's not (only) the encryption that needs optimizing but rather the whole process. \$\endgroup\$ – t3chb0t Jul 19 '17 at 11:43
  • \$\begingroup\$ @t3chb0t thank you, you gave a good idea. You are so mean link \$\endgroup\$ – Lolidze Jul 19 '17 at 13:09
  • 1
    \$\begingroup\$ Three minor notes: 1. Rfc2898DeriveBytes implements IDisposable and therefore should be wrapped in a using construct. 2. .CreateEncryptor() creates an IDisposable resource. It should be pulled out and wrapped in a using construct. 3. cs.Close(); isn't needed as the using scope will close and dispose it immediately thereafter. \$\endgroup\$ – Jesse C. Slicer Jul 19 '17 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.