title: C# Encrypt serialized file before writing to disk
body: <p>Let's say my program has a class called &quot;customer&quot; and the customer class is serializable so I can read and write it to disk. The customer class holds sensitive information that I want to encrypt, the only way I know I could keep the file safe would be to:</p>
  <blockquote>
  <p>1-Serialize the file to disk</p>
  <p>2-Reopen and load the file</p>
  <p>3-Encrypt the file</p>
  <p>4-Rewrite file to disk</p>
  </blockquote>
  <p>This would work, but there is a risk that the file could be intercepted in it's unencrypted state and furthermore this is just really inefficient.</p>
  <p>Instead I would like to:</p>
  <blockquote>
  <p>1-Create file in memory</p>
  <p>2-Encrypt file in memory</p>
  <p>3-Write encrypted file to disk</p>
  </blockquote>
  <p>Is this possible? If it is how? Thanks in advance.</p>