Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculation of encrypted file size does not match true size
    primarykey
    data
    text
    <p>I have the need to calculate the size of a file I am encrypting using Rijndael.</p> <p>According to other answers on this site, and on google, the following is the correct way of calculating encrypted data length:</p> <pre><code>EL = UL + (BS - (UL Mod BS) Mod BS) Where: EL = Encrypted Length UL = Unencrypted Length BS = Block Size </code></pre> <p>In my instance, the <strong>unencrypted file length is 5,101,972 bytes</strong>, and I am using a 128bit encryption key, giving me a <strong>block size of 16 bytes</strong>. Therefore, the equation is:</p> <pre><code>EL = 5101972 + (16 - (5101972 Mod 16) Mod 16) EL = 5101972 + (16 - 4 Mod 16) EL = 5101972 + (12 Mod 16) EL = 5101972 + 12 EL = 5101984 </code></pre> <p>Giving an <strong>encrypted file length of 5,101,984 bytes</strong>.</p> <p>However, the size of my file after encryption weighs in at 5,242,896 A massive difference in sizes of 140,912 bytes!</p> <p>Now.. I'm obviously doing SOMETHING wrong, but I can't work out what it is. Below is my encryption and decryption test code, as well as the method used to calculate the encrypted size:</p> <pre><code>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.Zeros}; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(passwordBytes, IVBytes); using (CryptoStream cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write)) { for (long i = 0; i &lt; fsInput.Length; i += chunkSize) { byte[] chunkData = new byte[chunkSize]; int bytesRead = 0; while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) &gt; 0) { cryptoStream.Write(chunkData, 0, chunkSize); } } } } } } 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.Zeros}; ICryptoTransform decryptor = symmetricKey.CreateDecryptor(passwordBytes, IVBytes); using (CryptoStream cryptoStream = new CryptoStream(fsOutput, decryptor, CryptoStreamMode.Write)) { for (long i = 0; i &lt; fsInput.Length; i += chunkSize) { byte[] chunkData = new byte[chunkSize]; int bytesRead = 0; while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) &gt; 0) { cryptoStream.Write(chunkData, 0, bytesRead); } } fsOutput.SetLength(fileLength); } } } } private static void CalcEncSize(string decryptedFileName) { FileInfo fi = new FileInfo(decryptedFileName); if (fi.Exists) { long blockSize = 128/8; long fileLength = fi.Length; long encryptedSize = fileLength + ((blockSize - (fileLength % blockSize)) % blockSize); encryptedSize += 24; //16 bytes for the IV, and 8 more for the filelength, both stored at the start of the file. Console.WriteLine("Estimated Encryption Size: " + encryptedSize.ToString()); } } </code></pre> <p>Note: In the calculations at the start, I am NOT including the extra 24 bytes that are used at the start of the encrypted file by myself to store the original file length, and the IV... I know this, but didn't want to complicate the equation more than necessary.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload