Java Is secure deletion possible in Java

  • Thread starter Thread starter Paul Uszak
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
Secure deletion in Java is complex due to the way operating systems handle file storage. Writing over a file may not guarantee that the original data is overwritten on the same physical disk block, especially with modern SSDs, which manage data differently and may not allow for reliable overwriting. While Java can perform file operations, achieving true secure deletion requires low-level access that is often not possible through high-level APIs. Additionally, external factors like OS caching and the potential for adversaries to restore deleted files complicate the process. For effective secure deletion, full disk encryption or specialized tools may be necessary, as relying solely on Java may not meet security needs.
  • #31
rootone said:
Sure, an OS can (and so it should!), prevent somebody from attempting to meddle with storage which it has deemed protected. I think though that the OP really meant secure deletion of just regular files using Java. I was suggesting that one way to do it could be to provide Java with an external library designed for that purpose.

You still need to talk to/patch the OS. You would have to secure erase the swap file / page file just in case the OS decided to swap your file or decryption key to disk (which is one of the reasons why I would recommend full disk encryption for this). I've not looked at the source code for PGP but I am fairly sure it must have some kind of OS-level memory lock to prevent paging. If you did that without collaboration with the OS, you'd be asking for a system crash. Obviously you can do this in an external library, but the point is that it would probably be on the same level as the OS itself.
 
Technology news on Phys.org
  • #32
Yes, very easily
step 1. open file stream with pointer to location.
step 2. spam write random values for that file.
step 3. delete file
step 4. repeat steps 2 and 3,
I have done this in C# before, found this on codeproject it seems legit. Should be easy enough to translate into java.
Code:
public void WipeFile(string filename, int timesToWrite)
{
    try
    {
        if (File.Exists(filename))
        {
            // Set the files attributes to normal in case it's read-only.

            File.SetAttributes(filename, FileAttributes.Normal);

            // Calculate the total number of sectors in the file.
            double sectors = Math.Ceiling(new FileInfo(filename).Length/512.0);

            // Create a dummy-buffer the size of a sector.

            byte[] dummyBuffer = new byte[512];

            // Create a cryptographic Random Number Generator.
            // This is what I use to create the garbage data.

            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            // Open a FileStream to the file.
            FileStream inputStream = new FileStream(filename, FileMode.Open);
            for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
            {
                UpdatePassInfo(currentPass + 1, timesToWrite);

                // Go to the beginning of the stream

                inputStream.Position = 0;

                // Loop all sectors
                for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
                {
                    UpdateSectorInfo(sectorsWritten + 1, (int) sectors);

                    // Fill the dummy-buffer with random data

                    rng.GetBytes(dummyBuffer);

                    // Write it to the stream
                    inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
                }
            }

            // Truncate the file to 0 bytes.
            // This will hide the original file-length if you try to recover the file.

            inputStream.SetLength(0);

            // Close the stream.
            inputStream.Close();

            // As an extra precaution I change the dates of the file so the
            // original dates are hidden if you try to recover the file.

            DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0);
            File.SetCreationTime(filename, dt);
            File.SetLastAccessTime(filename, dt);
            File.SetLastWriteTime(filename, dt);

            // Finally, delete the file

            File.Delete(filename);

            WipeDone();
        }
    }
    catch(Exception e)
    {
        WipeError(e);
    }
}
 
  • #33
Superposed_Cat said:
Yes, very easily
step 1. open file stream with pointer to location.
step 2. spam write random values for that file.
step 3. delete file
step 4. repeat steps 2 and 3,
I have done this in C# before, found this on codeproject it seems legit. Should be easy enough to translate into java.

If you had read the thread, you would realize why this is NOT secure.

TL;DR: On an SSD you have no guarantee that you are over-writing the disk block that you think you are. By design, the controller in an SSD does not always map the same disk address to the same memory circuit. There are some other minor issues like inodes on ext filesystems.

The rest of it is stuff related to the OP's specfic problem; in his case he also has to make sure the OS does not swap/page the data to disk as that would create a copy of it in the swap/page file.
 
Last edited:
  • #34
Carno Raar said:
If you had read the thread, you would realize why this is NOT secure.

TL;DR: On an SSD
Sorry my post was for HDD's
 

Similar threads

Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
16K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 27 ·
Replies
27
Views
10K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
14
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K