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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:57:00+00:00 2026-05-16T01:57:00+00:00

I’m in a situation where I need to encrypt / decrypt a file of

  • 0

I’m in a situation where I need to encrypt / decrypt a file of n length securely, ideally using Rijndael, but definitely at 256bit encryption.

I’ve played around with encryption before and have encrypted/decrypted strings and byte arrays quite happily.
However, because I don’t know the size of the file (and it’s very feasible that the files in question could be quite large (~2.5gb) I can’t just load them up into a byte array and enc/decrypt them in a single bound as I have before.

So, after a bit of reading around on Google, I knew the answer was to encrypt and decrypt the file in chunks, and so I produced the following code:

private static void Enc(string decryptedFileName, string encryptedFileName)
{            
   FileStream fsOutput = File.OpenWrite(encryptedFileName);
   FileStream fsInput = File.OpenRead(decryptedFileName);

   byte[] IVBytes = Encoding.ASCII.GetBytes("1234567890123456");

   fsOutput.Write(BitConverter.GetBytes(fsInput.Length), 0, 8);
   fsOutput.Write(IVBytes, 0, 16);

   RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC};
   ICryptoTransform encryptor = symmetricKey.CreateEncryptor(passwordDB.GetBytes(256 / 8), IVBytes);
   CryptoStream cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write);

   for (long i = 0; i < fsInput.Length; i += chunkSize)
   {
      byte[] chunkData = new byte[chunkSize];
      fsInput.Read(chunkData, 0, chunkSize);
      cryptoStream.Write(chunkData, 0, chunkData.Length);
   }
   cryptoStream.Close();
   fsInput.Close();
   fsInput.Dispose();
   cryptoStream.Dispose();
}

private static void Dec(string encryptedFileName, string decryptedFileName)
{
    FileStream fsInput = File.OpenRead(encryptedFileName);
    FileStream fsOutput = File.OpenWrite(decryptedFileName);

    byte[] buffer = new byte[8];
    fsInput.Read(buffer, 0, 8);

    long fileLength = BitConverter.ToInt64(buffer, 0);

    byte[] IVBytes = new byte[16];
    fsInput.Read(IVBytes, 0, 16);

    RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC };
    ICryptoTransform decryptor = symmetricKey.CreateDecryptor(passwordDB.GetBytes(256 / 8), IVBytes);
    CryptoStream cryptoStream = new CryptoStream(fsOutput,decryptor,CryptoStreamMode.Write);

    for (long i = 0; i < fsInput.Length; i += chunkSize)
    {
        byte[] chunkData = new byte[chunkSize];
        fsInput.Read(chunkData, 0, chunkSize);
        cryptoStream.Write(chunkData, 0, chunkData.Length);
    }
    cryptoStream.Close();
    cryptoStream.Dispose();
    fsInput.Close();
    fsInput.Dispose();                      
} 

It all “looks” good to me, but sadly looks appear to be deceiving!

Encryption works without error, but during decryption, the “cryptoStream.Close()” method throws the following exception:

System.Security.Cryptography.CryptographicException
was unhandled Message=”Padding is
invalid and cannot be removed.”
Source=”mscorlib” StackTrace:
at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[]
inputBuffer, Int32 inputOffset, Int32
inputCount, Byte[]& outputBuffer,
Int32 outputOffset, PaddingMode
paddingMode, Boolean fLast)
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32
inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean
disposing)
at System.IO.Stream.Close()

It also appears that the unencrypted file size isn’t matching the file size expected (ranging from around 8 bytes, to around 60)

I “fixed” the exception by altering the RijndaelManaged object creation lines to include a padding type, as below:

RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC,Padding=PaddingMode.None };

But the file sizes still don’t match up and, predictably, the freshly unencrypted file is baloney!

I will admit that I’m now outside of my comfort zone with encryption/decryption, and it’s probably a rookie mistake – but I can’t spot it!

Any help on resolving this would be greatly appreciated!

  • 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-16T01:57:02+00:00Added an answer on May 16, 2026 at 1:57 am

    The problem is that I was using:

    passwordDB.GetBytes(256 / 8)
    

    within the constructor for the RijndaelManaged object in both the Encryption and Decryption methods, and I wasn’t re-initialising the passwordDB object before attempting to decrypt.

    The resolution was to simply including the construction of the passwordDB object within the first lines of both the Enc and Dec methods, as follows:

            private static void Enc(string decryptedFileName, string encryptedFileName)
            {
                PasswordDeriveBytes passwordDB = new PasswordDeriveBytes("ThisIsMyPassword", Encoding.ASCII.GetBytes("thisIsMysalt!"), "MD5", 2);
                byte[] passwordBytes = passwordDB.GetBytes(128 / 8);
    
                using (FileStream fsOutput = File.OpenWrite(encryptedFileName))
                {
                    using(FileStream fsInput = File.OpenRead(decryptedFileName))
                    {
                        byte[] IVBytes = Encoding.ASCII.GetBytes("1234567890123456");
    
                        fsOutput.Write(BitConverter.GetBytes(fsInput.Length), 0, 8);
                        fsOutput.Write(IVBytes, 0, 16);
    
                        RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC,Padding=PaddingMode.ANSIX923};
                        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(passwordBytes, IVBytes);                   
    
                        using (CryptoStream cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write))
                        {
                            for (long i = 0; i < fsInput.Length; i += chunkSize)
                            {
                                byte[] chunkData = new byte[chunkSize];
                                int bytesRead = 0;
                                while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
                                {
                                    if (bytesRead != 16)
                                    {
                                        for (int x = bytesRead - 1; x < chunkSize; x++)
                                        {
                                            chunkData[x] = 0;
                                        }
                                    }
                                    cryptoStream.Write(chunkData, 0, chunkSize);
                                }
                            }
                            cryptoStream.FlushFinalBlock();
                        }
                    }
                }            
            }
    
            private static void Dec(string encryptedFileName, string decryptedFileName)
            {
                PasswordDeriveBytes passwordDB = new PasswordDeriveBytes("ThisIsMyPassword", Encoding.ASCII.GetBytes("thisIsMysalt!"), "MD5", 2);
                byte[] passwordBytes = passwordDB.GetBytes(128 / 8);
    
                using (FileStream fsInput = File.OpenRead(encryptedFileName))
                {
                    using (FileStream fsOutput = File.OpenWrite(decryptedFileName))
                    {
                        byte[] buffer = new byte[8];
                        fsInput.Read(buffer, 0, 8);
    
                        long fileLength = BitConverter.ToInt64(buffer, 0);
    
                        byte[] IVBytes = new byte[16];
                        fsInput.Read(IVBytes, 0, 16);
    
    
                        RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC,Padding=PaddingMode.ANSIX923};
                        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(passwordBytes, IVBytes);
    
                        using (CryptoStream cryptoStream = new CryptoStream(fsOutput, decryptor, CryptoStreamMode.Write))
                        {
                            for (long i = 0; i < fsInput.Length; i += chunkSize)
                            {
                                byte[] chunkData = new byte[chunkSize];
                                int bytesRead = 0;
                                while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
                                {
                                    cryptoStream.Write(chunkData, 0, bytesRead);
                                }
                            }
                        }
                    }
                }
            }
    

    Knew it had to be a schoolboy error 😛

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

Sidebar

Related Questions

No related questions found

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.