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 7504113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:27:48+00:00 2026-05-29T21:27:48+00:00

I have a two very simple methods to encrypt/decrypt a byte[] using the AesCryptoServiceProvider.

  • 0

I have a two very simple methods to encrypt/decrypt a byte[] using the AesCryptoServiceProvider. But I’m very surprised about the performance of the method.

I tried to encrypt and decrypt as many bytes[] as my computer can and in the first 4 seconds I can encrypt about 2000 times, but in the next 4 seconds, about 1000, in the next four seconds, about 500… at the end i can have about 80 operations per 4 seconds. Why?

Look at the code.

namespace Encrypttest
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    using System.Globalization;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("AES provider");

            var key = GenerateKey();
            DateTime now = DateTime.Now.AddSeconds(2);
            var toStop = now.AddSeconds(120);
            long operations = 0;

            byte[] buffer = new byte[16];
            byte[] result = new byte[buffer.Length];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 2;
            }

            for (long i = 0; i < 10000000; i++)
            {
                result = Encrypt(buffer, key);
                Decrypt(result, key);
                buffer = result;
                operations++;

                if (DateTime.Now > now)
                {
                    Console.WriteLine(now.ToLongTimeString() + ";" + operations + ";" + System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64);
                    operations = 0;
                    now = DateTime.Now.AddSeconds(2);
                }

                if (toStop < DateTime.Now)
                {
                    break;
                }
            }
        }

        /// <summary>
        /// Encrypts the specified data.
        /// </summary>
        /// <param name="data">The data to encrypt</param>
        /// <param name="key">The key to encrypt data.</param>
        /// <returns>
        /// The data encrypted.
        /// </returns>
        public static byte[] Encrypt(byte[] data, SymmetricKey key)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException("data");
            }

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }


            using (AesCryptoServiceProvider providerInLine = new AesCryptoServiceProvider())
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (ICryptoTransform cryptoEncryptor = providerInLine.CreateEncryptor(key.Key, key.IV))
                    {
                        using (CryptoStream writerStream = new CryptoStream(stream, cryptoEncryptor, CryptoStreamMode.Write))
                        {
                            writerStream.Write(data, 0, data.Length);
                            writerStream.FlushFinalBlock();
                            return stream.ToArray();
                        }
                    }
                }
            }

        }


        /// <summary>
        /// Decrypts the specified data.
        /// </summary>
        /// <param name="data">The data to decrypt</param>
        /// <param name="key">The key to decrypt data.</param>
        /// <returns>
        /// The data encrypted.
        /// </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
        public static byte[] Decrypt(byte[] data, SymmetricKey key)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException("data");
            }

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }


            using (AesCryptoServiceProvider provider = new AesCryptoServiceProvider())
            {
                using (MemoryStream memStreamEncryptData = new MemoryStream(data))
                {
                    using (ICryptoTransform cryptoDecryptor = provider.CreateDecryptor(key.Key, key.IV))
                    {
                        using (CryptoStream stream = new CryptoStream(memStreamEncryptData, cryptoDecryptor, CryptoStreamMode.Read))
                        {
                            byte[] plainTextBytes = new byte[data.Length];
                            stream.Read(plainTextBytes, 0, plainTextBytes.Length);
                            return plainTextBytes;
                        }
                    }
                }
            }

        }

        /// <summary>
        /// Generates a random key and initialization vector
        /// </summary>
        /// <returns>
        /// The key and initialization vector.
        /// </returns>
        public static SymmetricKey GenerateKey()
        {
            using (AesCryptoServiceProvider provider = new AesCryptoServiceProvider())
            {
                provider.GenerateKey();
                SymmetricKey key = new SymmetricKey(provider.Key, provider.IV);
                return key;
            }
        }
    }

    public class SymmetricKey
    {
        /// <summary>
        /// The key.
        /// </summary>
        private byte[] key;

        /// <summary>
        /// The initialization vector.
        /// </summary>
        private byte[] iv;

        /// <summary>
        /// Initializes a new instance of the <see cref="SymmetricKey"/> class.
        /// </summary>
        public SymmetricKey()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SymmetricKey"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="iv">The iv.</param>
        public SymmetricKey(byte[] key, byte[] iv)
        {
            this.Init(key, iv);
        }

        /// <summary>
        /// Gets the key.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Byte[] is what the providers need")]
        public byte[] Key
        {
            get { return this.key; }
        }

        /// <summary>
        /// Gets the iv.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Byte[] is what the providers need")]
        public byte[] IV
        {
            get { return this.iv; }
        }

        /// <summary>
        /// Loads the specified key and iv
        /// </summary>
        /// <param name="newKey">The key.</param>
        /// <param name="newIV">The iv.</param>
        public void Init(byte[] newKey, byte[] newIV)
        {
            this.key = newKey;
            this.iv = newIV;
        }
    }
}
  • 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-05-29T21:27:50+00:00Added an answer on May 29, 2026 at 9:27 pm

    The ciphertext is 16 bytes larger than the plaintext due to padding. Since you’re using the ciphertext of of one iteration as the plaintext of the next, the length of the buffer grows linearly with the number of iterations so far. This explains the slowdown.

    After 2000 iterations it needs to encrypt 30kB per iteration, after 6000 iterations it needs to encrypt 100kB per iteration…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple table with two columns, but has 4.5M rows. CREATE
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have two very simple, identical UITableViews in my app that are populated with
The situation is very simple, I have two panel. In the event of OnMouseOver
I have two very simple tables. Product and ProductCategory (ProductCategory is like a 'lookup'
I have two (very large) text files. What is the fastest way - in
I have two question to put forward: I was very interested, even intrigued by
I have two arrays, one is very large (more than million entries) and other
I have a very strange issue on my hands. I have two IIS websites
I'm not very good in SQL and HQL... I have two domains: class Hotel

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.