Android Question SAFE REMOVAL OF FILES?

T201016

Active Member
Licensed User
Longtime User
Hello all!
What do you think about these two different developed procedures for removing files safely?
- Which of these versions is better for you?
I think both solutions are reasonable only for small files up to eg. 100Kb.
What if it comes to remove some image files above 1MB ?!

Can anyone have a better solution with the additional blurring information in metadata for Android - (B4A)?

------ Example 1:
B4X:
Sub Delete(fPath As String, fName As String) As Boolean
    Try
        If File.Exists(fPath, fName) Then
            Private raf As RandomAccessFile
            Dim sr As SecureRandom

            raf.Initialize(fPath,fName, False)
            Dim aesKey(64) As Byte '64 bytes = 4x128 bits
            Dim total As Long
            Do While (raf.CurrentPosition < raf.Size)
                DoEvents
               
                total = raf.Size

                sr.SetRandomSeed(total)
                sr.GetRandomBytes(aesKey)
                raf.WriteObject(aesKey, False, raf.CurrentPosition)
            Loop
            raf.Flush
            raf.Close
            File.Delete(fPath,fName)
        End If
        Return True
    Catch
        Return False
    End Try
End Sub

------ Example 2:
B4X:
package software.b4a.saferemoval;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.*;
import java.nio.channels.*;
import java.security.SecureRandom;

@BA.Version(1.52F)
@BA.ShortName("SafeDelete")
@Author("noname")

public class SafeDelete
{
    public int type = 0;

    /**
     * Delete Directory and files.
     * Safe = 0; Normal = 1;
     */
    public void Initialize(int number)
    {
        type = number;
    }
    public boolean DeleteDirectory(String srcPath) throws IOException
    {
        return DeleteDir(new File(srcPath));
    }
    public boolean DeleteDir(File path) throws IOException
    {
        if(path.exists())
        {
            File[] files = path.listFiles();
            for(int i=0; i<files.length; i++)
            {
                if(files[i].isDirectory())
                {
                    DeleteDir(files[i]);
                }
                else
                {
                    if(type == 0)
                    {
                        long length = files[i].length();
                        SecureRandom ranGen = new SecureRandom();
                        RandomAccessFile raf = new RandomAccessFile(files[i], "rws");
                        raf.seek(0);
                        raf.getFilePointer();
                        byte[] aesKey = new byte[64]; // 64 bytes = 4x128 bits
                        int pos = 0;
                        while (pos < length)
                        {
                            ranGen.nextBytes(aesKey);
                            raf.write(aesKey);
                            pos += aesKey.length;
                        }
                        raf.close();
                        files[i].delete();
                    }
                    else
                    {
                        files[i].delete();
                    }
                }
            }
        }
        return(path.delete());
    }

    /**
     * Secure delete file.
     * Example 1 - overwrite with AES data.
     */
    public boolean DeleteFile(String srcPath) throws IOException
    {
     try
     {
        File src = new File(srcPath);
        if (src.exists())
        {
            long length = src.length();
            SecureRandom ranGen = new SecureRandom();
            RandomAccessFile raf = new RandomAccessFile(src, "rws");
            raf.seek(0);
            raf.getFilePointer();
            byte[] aesKey = new byte[64]; // 64 bytes = 4x128 bits
            int pos = 0;
            while (pos < length)
            {
                ranGen.nextBytes(aesKey);
                raf.write(aesKey);
                pos += aesKey.length;
            }
            raf.close();
            src.delete();
            return true;
        }
        else
        {
          return false;
        }
     }
     catch (Exception e)
     {
       Log.e("SafeDelete", "Could not delete file " + e.getMessage());
       return false;
     }
    }

    /**
     * Secure delete file.
     * Example 2 - overwrite with random data.
     */
    public boolean DeleteFile2(String srcPath) throws IOException
    {
     try
     {
        File src = new File(srcPath);
        if (src.exists())
        {
            SecureRandom random = new SecureRandom();
            RandomAccessFile raf = new RandomAccessFile(src, "rws");
            FileChannel channel = raf.getChannel();
            MappedByteBuffer buffer
            = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
            // overwrite with zeros
            while (buffer.hasRemaining())
            {
                buffer.put((byte) 0);
            }
            buffer.force();
            buffer.rewind();
            // overwrite with ones
            while (buffer.hasRemaining())
            {
                buffer.put((byte) 0xFF);
            }
            buffer.force();
            buffer.rewind();
            // overwrite with random data; one byte at a time
            byte[] data = new byte[1];
            while (buffer.hasRemaining())
            {
                random.nextBytes(data);
                buffer.put(data[0]);
            }
            buffer.force();
            src.delete();
            return true;
        }
        else
        {
          return false;
        }
     }
     catch (Exception e)
     {
       Log.e("SafeDelete", "Could not delete file " + e.getMessage());
       return false;
     }
    }
}

Regards
 

T201016

Active Member
Licensed User
Longtime User
Thanks to Erel for a valuable hint ...
I thought of just using zero arrays, although I was not entirely sure of that, so it's a question to all of us.

Thanks again.
PS. I still have an improved algorithm to speed up the erase process, but so far no idea.
 
Upvote 0
Top