Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You shouldn't write to the output file before the actual encryption process. The <code>CryptoStream</code> will handle all the necessary padding when it is closed. Also the for loop isn't necessary as the inner while loop will read the entire file. Also you should only write as much as was read from the file. Try these changes.</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"); 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)) { byte[] chunkData = new byte[chunkSize]; int bytesRead = 0; while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) &gt; 0) { cryptoStream.Write(chunkData, 0, bytesRead); //KEY FIX } } } } } </code></pre> <p>[edit]</p> <p>Oh I've missed out on a lot of information. I misread what your sizes were thinking 140,912 was the size, not difference. Now that I see that, I can make more intelligible response. Based on your code, the difference in sizes should be comparable to your chunk size. Since the chunkSize can be somewhat large, your code will typically write up to chunkSize more data than is actually in your input file (as Greg and caf pointed out). The line that I've marked is <em>the</em> reason for the discrepancy.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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