Is secure deletion possible in Java

  • Context: Java 
  • Thread starter Thread starter Paul Uszak
  • Start date Start date
  • Tags Tags
    Java
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
33 replies · 8K views
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.
 
Physics news on Phys.org
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);
    }
}
 
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:
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