I'm trying to make an app that can encrypt files and decrypt them on demand(for educational purposes) I need a way to:

  1. delete the file from where it was before
  2. delete the file each time the app decrypts it and the user is done using the file

It's essential that the decrypted form of the files can't be recovered. I'm open to any ideas / alternative ways I can achieve this. I'm using C# if it helps

  • 2
    Without controle over the underlying file systems. You can’t. All you can do is make it secure enough. And that means in memory decryption…. And nothing on disk.
    – LvB
    Feb 15, 2022 at 21:36
  • You can try to implement something along the lines of SDelete. It is a tool that is part of Windows SysInternals. The source code is not available, but there is information about how it works. Also, you might have an easier time controlling the lower-level details of the Windows API if you write your app in C rather than C#. For example, you can force it not to cache writes from the C API, but I'm not sure if you can do that from the C# API.
    – hft
    Feb 16, 2022 at 22:57

2 Answers 2


You can't. You're entirely beholden to the implementation details of the storage stack below your application, which is opaque to you.

Here are some examples of scenarios in which the original data would remain latent:

  • The user could have a cloud backup solution that keeps copies of their files online.
  • Volume shadow copies could be configured, meaning that the file's historical contents are retained.
  • If the file is an image, its thumbnail may be stored in a thumbnail database maintained by the OS shell.
  • Cached copies of the original file data may be stored in memory by the operating system.
  • The file you're encrypting might not be on local storage (e.g. it might be on an SMB share or iSCSI volume), meaning you have no control over the backups or redundancy that might be present.
  • The filesystem implementation is likely to utilise soft-deletion, in which a delete operation simply removes the file record and marks the blocks associated with the file data as free, ready for re-use, rather than wastefully overwriting the blocks.
  • The implementation of the filesystem may include journaling or other features that temporarily store file data on the disk outside the normal file structure.
  • SSDs and other flash storage devices may utilise wear-levelling and slack space, in which the physical flash cells are dynamically mapped to logical blocks that are presented to the system, and the physical amount of flash storage may exceed the amount of storage that is exposed to the user. This is done in order to increase the lifespan of the drive, because flash cells can only survive a certain number of write cycles. When you perform a write operation to a block, the SSD can choose to write that data to any physical flash cell that is marked as free in its internal map, rather than overwriting the cell that previously contained the data.
  • Storage redundancy architectures like RAID may retain copies of data in numerous ways.

This problem is non-trivial, and cannot be solved with a software application. Entire standards, such as NIST SP 800-88r1, have been written to govern safe methods of data destruction. Secure erasure of data requires careful security considerations to be made for the entire software and hardware stack, and must be supported by user behaviour (including organisational policy).

  • You can also add defragmentation and file updates (e.g. when you're working with a document and save it many times, multiple copies may exist on the disk) to the list. Feb 17, 2022 at 8:55

With everything @Polynomial has said it's still perfectly doable but only if we are talking about local storage and files have no other copies anywhere in the world.

First things first:

  • You take care of shadow copies either by deleting them or disabling them.
  • You also have to delete/disable page file/swap file as it may contain the copies or remnants of the file.


  • First you wipe the entirety of free space by creating a file and filling it with anything you want, including binary zeros. That will remove all the deleted copies of the file.
  • Second and that requires low level access to the disk, you go through all files whose sizes are not multiples of 4K and then you fill the rest of their very last clusters with binary zeros. In Windows this cannot be done in a normal session, in Unix OS'es it's possible.

SSD wear levelling is not a concern because to extract a file this way requires special equipment only OEMs have access to.

Not the answer you're looking for? Browse other questions tagged .