N1tro Posted May 13, 2012 Posted May 13, 2012 (edited) Hi guys. Can someone explain why AES encryption with work large files lose or adding some bytes. 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 May 13, 2012 by N1tro
kao Posted May 13, 2012 Posted May 13, 2012 (edited) 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.htmlEDIT: added examples Edited May 13, 2012 by kao 1
N1tro Posted May 13, 2012 Author Posted May 13, 2012 (edited) 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 51at 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 Edited May 13, 2012 by N1tro
deepzero Posted May 13, 2012 Posted May 13, 2012 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"... 1
ghandi Posted May 13, 2012 Posted May 13, 2012 (edited) 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) + 1becomesSize: 64 = ((16 * 3) + 1) + 15 // [16 - 1 = 15 for alignment]HR,Ghandi Edited May 13, 2012 by ghandi 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now