Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

C# AES Encryption (Large Files)

Featured Replies

Posted

Hi guys. Can someone explain why AES encryption with work large files lose or adding some bytes. :cc_confused:

Original size must be : 17,9 MB (18 819 479 bytes)

I get : 17,9 MB (18 819 488 bytes)

here code

public static void DataEncrypt(string fileIn, string fileOut)
{
FileStream FILE_In = new FileStream(fileIn, FileMode.Open, FileAccess.Read);
FileStream FILE_Out = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);byte[] TEST_KEY = new byte[]
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
byte[] TEST_IV = new byte[]
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};Rijndael AES = Rijndael.Create();AES.Key = TEST_KEY;
AES.IV = TEST_IV;CryptoStream cs = new CryptoStream(FILE_Out, AES.CreateEncryptor(), CryptoStreamMode.Write);int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead;
do
{
bytesRead = FILE_In.Read(buffer, 0, bufferLen);
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);//cs.FlushFinalBlock();
cs.Close();
FILE_In.Close();
}

Thanks Advance.

Edited by N1tro

AES has a fixed block size of 128 bits (16 bytes). So, any time you encrypt something, it will be padded to 16-bytes boundary.

See explanation and examples here: http://www.di-mgt.com.au/cryptopad.html

EDIT: added examples

Edited by kao

  • Author

Thanks for answer.With use PaddingMode.None;

Unhandled Exception: System.Security.Cryptography.CryptographicException: Length of the data to encrypt is invalid.
at System.Security.Cryptography.RijndaelManagedTransform.EncryptData(Byte[] i nputBuffer, 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()
at TDCDec.DataEncrypt(String fileIn, String fileOut) in D:\TEST\AESTest\AESTest\Program.cs:line 51
at TDCDec.Main(String[] args) in D:\TEST\AESTest\AESTest\Program.cs:line 17

Line 17

DataEncrypt(FILE_In, FILE_Out);

Line 51

cs.Close();

Sorry. Very bad know C# . On Pascal, everything looks much simpler but code slowly work :ermm:

Edited by N1tro

as kao said, it needs to be 16-byte aligned. See his link.

additionally, dont forget to close the out-file and free the buffers "buffer", "TEST_KEY" and "TEST_IV"...

Delphi/Pascal shouldn't be slower to do AES encryption than .NET, i would look at whatever implementation you have for bottlenecks or factors that will slow things down.

Just in case you're confused as to what deepzero and Kao mean by 'aligned', the size of the data to be encrypted/decrypted MUST be a multiple of 16 (evenly divisible) otherwise you need to make the buffer larger to account for the 'missing' bytes and add this extra to the size to be encrypted:

Size: 49 = (16 * 3) + 1

becomes

Size: 64 = ((16 * 3) + 1) + 15 // [16 - 1 = 15 for alignment]

HR,

Ghandi

Edited by ghandi

  • Author

Thanks guys. Problem Solved :)

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.