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

  • SEARCH
  • Home
  • 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 983671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:53:48+00:00 2026-05-16T04:53:48+00:00

I’ve created a class for encrypting and decrypting using AES. public class AesEncryptionProvider {

  • 0

I’ve created a class for encrypting and decrypting using AES.

public class AesEncryptionProvider {
    #region Fields

    // Encryption key
    private static readonly byte[] s_key = new byte[32] {
        // Omitted...
    };

    // Initialization vector
    private static readonly byte[] s_iv = new byte[16] {
        // Omitted...
    };

    private AesCryptoServiceProvider m_provider;
    private ICryptoTransform m_encryptor;
    private ICryptoTransform m_decryptor;

    #endregion

    #region Constructors

    private AesEncryptionProvider () {
        m_provider = new AesCryptoServiceProvider();
        m_encryptor = m_provider.CreateEncryptor(s_key, s_iv);
        m_decryptor = m_provider.CreateDecryptor(s_key, s_iv);
    }

    static AesEncryptionProvider () {
        Instance = new AesEncryptionProvider();
    }

    #endregion

    #region Properties

    public static AesEncryptionProvider Instance { get; private set; }

    #endregion

    #region Methods

    public string Encrypt (string value) {
        if (string.IsNullOrEmpty(value)) {
            throw new ArgumentException("Value required.");
        }

        return Convert.ToBase64String(
            Transform(
                Encoding.UTF8.GetBytes(value),
                m_encryptor));
    }

    public string Decrypt (string value) {
        if (string.IsNullOrEmpty(value)) {
            throw new ArgumentException("Value required.");
        }

        return Encoding.UTF8.GetString(
            Transform(
                Convert.FromBase64String(value),
                m_decryptor));
    }

    #endregion

    #region Private methods

    private byte[] Transform (byte[] input, ICryptoTransform transform) {
        byte[] output;
        using (MemoryStream memory = new MemoryStream()) {
            using (CryptoStream crypto = new CryptoStream(
                memory,
                transform,
                CryptoStreamMode.Write
            )) {
                crypto.Write(input, 0, input.Length);
                crypto.FlushFinalBlock();

                output = memory.ToArray();
            }
        }
        return output;
    }

    #endregion
}

As you can see, in both cases I’m writing to a MemoryStream via a CryptoStream. If I create a new decryptor via m_provider.CreateDecyptor(s_key, s_iv) on every call to Decrypt it works just fine.

What has gone wrong here? Why is the decryptor behaving as if its forgotten the IV? Is there something that the call to StreamReader.ReadToEnd() is doing that helps m_decryptor function correctly?

I would like to avoid either of the two “working” approaches I listed here as there is a performance hit on both and this is a very critical path. Thanks in advance.

  • 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-16T04:53:48+00:00Added an answer on May 16, 2026 at 4:53 am

    Ok, I admit I have no idea why this works, but change AesCryptoServiceProvider to AesManaged and voila.

    I also recommend making your class implement IDisposable as it contains three member variables which implement it. See below for code changes:

    public sealed class AesEncryptionProvider : IDisposable
    {
        // Encryption key
        private static readonly byte[] key = new byte[]
        {
            // Omitted...
        };
    
        // Initialization vector
        private static readonly byte[] iv = new byte[]
        {
            // Omitted...
        };
    
        private static readonly AesEncryptionProvider instance = new AesEncryptionProvider();
    
        private readonly AesManaged provider;
    
        private readonly ICryptoTransform encryptor;
    
        private readonly ICryptoTransform decryptor;
    
        private AesEncryptionProvider()
        {
            this.provider = new AesManaged();
            this.encryptor = this.provider.CreateEncryptor(key, iv);
            this.decryptor = this.provider.CreateDecryptor(key, iv);
        }
    
        public static AesEncryptionProvider Instance
        {
            get
            {
                return instance;
            }
        }
    
        public void Dispose()
        {
            this.decryptor.Dispose();
            this.encryptor.Dispose();
            this.provider.Dispose();
            GC.SuppressFinalize(this);
        }
    
        public string Encrypt(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("Value required.");
            }
    
            return Convert.ToBase64String(Transform(Encoding.UTF8.GetBytes(value), this.encryptor));
        }
    
        public string Decrypt(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("Value required.");
            }
    
            return Encoding.UTF8.GetString(Transform(Convert.FromBase64String(value), this.decryptor));
        }
    
        private static byte[] Transform(byte[] input, ICryptoTransform transform)
        {
            using (var memory = new MemoryStream())
            using (var crypto = new CryptoStream(memory, transform, CryptoStreamMode.Write))
            {
                crypto.Write(input, 0, input.Length);
                crypto.FlushFinalBlock();
                return memory.ToArray();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 486k
  • Answers 487k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have a variable name wrapped in string delimiters, making… May 16, 2026 at 8:11 am
  • Editorial Team
    Editorial Team added an answer Are you running this in a debug player? Looking at… May 16, 2026 at 8:11 am
  • Editorial Team
    Editorial Team added an answer Do you mean that you want all id1 records for… May 16, 2026 at 8:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.