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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:59:59+00:00 2026-05-18T19:59:59+00:00

I have a Byte[] field that is a file contents that I need to

  • 0

I have a Byte[] field that is a file contents that I need to encrypt. Nothing special or fancy, just enough to make sure the next person who gets it won’t be able to easily decode it without some effort. I would use the encryption that comes with .Net Framework 4.0 but I definitely do not need to make the file any bigger than it is.

I thought about just simply reversing the array or adding a few bytes to the end…?

If I can avoid making the array to much bigger that would be great.

Any suggestions?

Thanks!

  • 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-18T19:59:59+00:00Added an answer on May 18, 2026 at 7:59 pm

    Does the addition of 1-16 bytes hurt? AES will pad by default using the below method:

        private static void EncryptThenDecrypt(byte[] msg)
        {
            byte[] message = msg; // fill with your bytes
    
            if (message is null)
            {
                return;
            }
    
            byte[] encMessage; // the encrypted bytes
            byte[] decMessage; // the decrypted bytes - s/b same as message
            byte[] key;
            byte[] iv;
    
            using (SymmetricAlgorithm aes = Aes.Create())
            {
                if (aes is null)
                {
                    iv = key = null;
                    encMessage = Array.Empty<byte>();
                }
                else
                {
                    aes.GenerateKey();
                    aes.GenerateIV();
                    key = aes.Key;
                    iv = aes.IV;
                    encMessage = EncryptBytes(aes, message);
                }
            }
    
            using (SymmetricAlgorithm aes = Aes.Create())
            {
                if (aes is null || key is null)
                {
                    decMessage = Array.Empty<byte>();
                }
                else
                {
                    aes.Key = key;
                    aes.IV = iv;
                    decMessage = DecryptBytes(aes, encMessage);
                }
            }
    
            Debug.Assert(message.SequenceEqual(decMessage), "Decrypted bytes do not match original bytes.");
        }
    
        private static byte[] EncryptBytes(SymmetricAlgorithm alg, byte[] message)
        {
            if (message is null)
            {
    #pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
                return null;
    #pragma warning restore S1168 // Empty arrays and collections should be returned instead of null
            }
    
            if (message.Length == 0)
            {
                return message;
            }
    
            if (alg is null)
            {
                throw new ArgumentNullException(nameof(alg));
            }
    
            using (MemoryStream stream = new MemoryStream())
            using (ICryptoTransform encryptor = alg.CreateEncryptor())
            using (CryptoStream encrypt = new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
            {
                encrypt.Write(message, 0, message.Length);
                encrypt.FlushFinalBlock();
                return stream.ToArray();
            }
        }
    
        private static byte[] DecryptBytes(SymmetricAlgorithm alg, byte[] message)
        {
            if (message is null)
            {
    #pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
                return null;
    #pragma warning restore S1168 // Empty arrays and collections should be returned instead of null
            }
    
            if (message.Length == 0)
            {
                return message;
            }
    
            if (alg is null)
            {
                throw new ArgumentNullException(nameof(alg));
            }
    
            using (MemoryStream stream = new MemoryStream())
            using (ICryptoTransform decryptor = alg.CreateDecryptor())
            using (CryptoStream encrypt = new CryptoStream(stream, decryptor, CryptoStreamMode.Write))
            {
                encrypt.Write(message, 0, message.Length);
                encrypt.FlushFinalBlock();
                return stream.ToArray();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Byte[] field that is a file contents that I need to
I have a byte[] with the contents of file. I would like to send
I have a byte stream that may be UTF-8 data or it may be
I have a byte array filled from a file uploaded. But, in another part
I have a byte array that may or may not have null bytes at
I have a byte[] member in one of my persistable classes. Normally, I'd just
I have a GridView which has following template field that holds the filename, when
I have a table in Oracle that stores files as byte arrays in a
I have a large-ish file (4-5 GB compressed) of small messages that I wish
I am trying to download a file I have uploaded to an image field

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.