Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:53:11+00:00 2026-06-17T09:53:11+00:00

My code is private byte[] Invoke(Stream inputFileStream, CryptoAction action) { var msData = new

  • 0

My code is

     private byte[] Invoke(Stream inputFileStream, CryptoAction action)
     {
        var msData = new MemoryStream();
        CryptoStream cs = null;

        try
        {
            long inputFileLength = inputFileStream.Length;
            var byteBuffer = new byte[4096];
            long bytesProcessed = 0;
            int bytesInCurrentBlock = 0;

            var csRijndael = new RijndaelManaged();
            switch (action)
            {
                case CryptoAction.Encrypt:
                    cs = new CryptoStream(msData, csRijndael.CreateEncryptor(this.Key, this.IV), CryptoStreamMode.Write);
                    break;

                case CryptoAction.Decrypt:
                    cs = new CryptoStream(msData, csRijndael.CreateDecryptor(this.Key, this.IV), CryptoStreamMode.Write);
                    break;
            }

            while (bytesProcessed < inputFileLength)
            {
                bytesInCurrentBlock = inputFileStream.Read(byteBuffer, 0, 4096);
                cs.Write(byteBuffer, 0, bytesInCurrentBlock);
                bytesProcessed += bytesInCurrentBlock;
            }
            cs.FlushFinalBlock();

            return msData.ToArray();
        }
        catch
        {
            return null;
        }
    }

In case of encrypting large files of size 60mb System.OutOfMemoryException is thrown and program crashes.My operating system is 64bit and have 8Gb of ram.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:53:13+00:00Added an answer on June 17, 2026 at 9:53 am

    Try to get rid of all that buffer management code, that could be the cause of your problems… try to work with two streams (MemoryStream for volatile output is good):

    using (FileStream streamInput = new FileStream(fileInput, FileMode.Open, FileAccess.Read))
    {
        using (FileStream streamOutput = new FileStream(fileOutput, FileMode.OpenOrCreate, FileAccess.Write))
        {
            CryptoStream streamCrypto = null;
            RijndaelManaged rijndael = new RijndaelManaged();
            cspRijndael.BlockSize = 256;
    
            switch (CryptoAction)
            {
                case CryptoAction.ActionEncrypt:
                    streamCrypto = new CryptoStream(streamOutput, rijndael.CreateEncryptor(this.Key, this.IV), CryptoStreamMode.Write);
                    break;
    
                case CryptoAction.ActionDecrypt:
                    streamCrypto = new CryptoStream(streamOutput, rijndael.CreateDecryptor(this.Key, this.IV), CryptoStreamMode.Write);
                    break;
            }
    
            streamInput.CopyTo(streamCrypto);
            streamCrypto.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got next code: private Dictionary<int?, byte[]> GetAllLocalScanFiles() { using (DZine_IStylingEntities ctxLocal = new
following is my code: private byte[] downloadImage(String image_url) { byte[] image_blob = null; URL
my code : private AtomicBoolean fetched1 = new AtomicBoolean(false); private int rowCount; public int
I have the following code: private void _DoValidate(object sender, DoWorkEventArgs e) { this.BeginInvoke(new MethodInvoker(()
Client Code: TcpClient client = new TcpClient(); NetworkStream ns; private void Form1_Load(object sender, EventArgs
I have the following code: private string Connect() { string responseData; try { TcpClient
I have this code: private void SaveStreamToFile(string fileFullPath, Stream stream) { if (stream.Length ==
I found the following code on the web: private byte [] StreamFile(string filename) {
I'm trying to serialize/deserialize string. Using the code: private byte[] StrToBytes(string str) { BinaryFormatter
Hi I have some strings generated using the following code: private static string CalcHashCode(byte[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.