title: Using ProtectedData.Protect() for files
body: <p>I'm working on some code that ideally can encrypt a file using the Windows DPAPI. This is fine if the file can be read directly into memory, but if it can't I cannot encrypt it. <code>ProtectedData.Protect</code> takes 3 arguments - the data as a byte array, optional entropy as a byte array, and a scope. </p>
  
  <p>When I use it as below, it yields 2 different results:</p>
  
  <pre><code>var data = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
  
  byte[] encryptedDataA = ProtectedData.Protect(data, null, DataProtectionScope.CurrentUser);
  
  byte[] encryptedDataB = ProtectedData.Protect(data, null, DataProtectionScope.CurrentUser);
  
  Console.WriteLine(encryptedDataA.SequenceEqual(encryptedDataB));
  
  // False!!!!
  </code></pre>
  
  <p>Specifying identical entropy also results in different results. <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/54711fdf-7ace-408f-a7b5-91b670aac8f3/securitycryptographyprotecteddataprotect-never-returning-same-encrypted-value-net-40?forum=clr" rel="nofollow noreferrer">Here</a> is an MSDN answer corroborating this. However, there is no documentation I can find detailing the header or how to read it.</p>
  
  <p>As there are no overloads taking it as anything but a <code>byte[]</code>, I can't find anyway of fixing this to allow for encryption of large files without ugly chunking of the data.</p>
  
  <p>Is there any way to work around this?</p>