Page 1 of 1

Secure File Deletion in VB.NET Rate Topic: -----

#1 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Posted 05 July 2010 - 12:18 PM

First off: What is secure file deletion?

Secure file deletion is a way of deleting a file, so that it cannot be recovered*. When you delete a file from the Recycle Bin, you'd assume that it's gone forever. This is incorrect. When you delete a file from the recycle bin, it just tells windows that the file can be over-written if windows needs the space. Anyobody can come along and recover it, easily using a tool such asRecuva by Piriform.
Secure file deletion over-writes a file before it deletes it from the recycle bin - so the original contents of the files are virtually impossible to recover. In this tutorial, I will show you how to securely delete a file in VB.NET.



*A message to all those who are pros at secure file deletion:
I know that if the FBI wants what's on your hard drive, and it's been securely deleted with a method like this, then they'll probably be able to recover some of it with hardware based tools. But to the average Joe, this will work fine.


Okey, so first we want to start up Visual Basic (I use Visual Basic 2010 Express Edition). Then we need to create a new Windows Forms Application. Call it whatever you want (I called it SecureDelete).

Now, Add a textbox, and two buttons to your form. Name the first button 'btnChoose', the second button 'btnDelete', and the textbox 'txtFilePath'.

Now, switch to the code view, and outside of any events, add this code:
Dim ofd As New OpenFileDialog


This code will create a new OpenFileDialog for us, so the user can choose which file to delete.


Next, go back to your form, and double click 'btnChoose'. Add this code in the Button_Click event:
  'Make the title say "Choose File to Delete"
  ofd.Title = "Choose File to Delete"
  'Make sure that the user can choose any file
  ofd.Filter = "All Files|*.*"
  'Show the OpenFileDialog, and if the user presses OK, 
  'then set the text of the file path textbox to the path of the selected file.
  If ofd.ShowDialog = DialogResult.OK Then
     txtFilePath.Text = ofd.FileName
  End If


Basically we just asked the user which file they wanted to delete, and then set the text in the textbox to the filepath of that file.

This is where stuff gets a little more complex:
Next, go back to your form, and double click 'btnDelete'. Add this code in the Button_Click event:
        'First we need to check if the filepath in the textbox exists
        If My.Computer.FileSystem.FileExists(txtFilePath.Text) Then
            'Then we want to declare the number of times we want to over write the file:
            Dim passes As Integer = 0
            'We might encounter some errors in this section, such as "Access Denied", or something, so we need to use a Try...Catch statement.
            Try
                'In this case we're going to over-write the file ten times.
                Do Until passes = 10
                    passes += 1
                    'Create a new Byte Array for all the bytes in the file
                    Dim array = IO.File.ReadAllBytes(txtFilePath.Text)
                    'A little messy, but this code will just replace each charactor in the array with "1" or "0".
                    Dim OneOrZero As Integer = 0
                    Dim all As String = String.Empty
                    'For each byte in the array...
                    For Each selbyte In array
                    'Change it to one or zero...
                        If OneOrZero = 0 Then
                            selbyte = "0"
                            OneOrZero = 1
                        ElseIf OneOrZero = 1 Then
                            selbyte = "1"
                            OneOrZero = 0
                        End If
                        all &= selbyte
                    Next
                    'Write the new contents of the file to the file
                    Dim SaveF As New IO.StreamWriter(txtFilePath.Text)
                    SaveF.WriteLine(all)
                    SaveF.Close()
                    'Loop through untill we have completed the procedure 10 times...
                Loop
                'Finally delete the file.
                My.Computer.FileSystem.DeleteFile(txtFilePath.Text, False, False)
            Catch ex As Exception
                'If we encounter any errors in the process, show a dialog
                MessageBox.Show("Error: " & ex.Message, "Error")
            End Try
        Else
            'If the file does not exist, then show a dialog.
            MessageBox.Show("Error: the file specified does not exist. Please check the file name and try again", "Error")
        End If



So what did we do there?
Basically, firstly, we checked to see if the filepath in the textbox existed. Then if it did, we created a byte array with all the data in the file in it. Then we replaced all of that with ones and zeroes, and saved that to the file. We repeated this procedure ten times.


Notes & Possible errors:

If the file which you are over-writing is large, then the program may not respond for a variable amount of time, depending upon how large the file is. This can be circumvented by using a backgroundworker.

This file deletion method is very basic, but it's the basis for a program.


Thanks for reading my tutorial - I hope it helped. There's lots more on web browsers coming whenever I have time to write about it!

Is This A Good Question/Topic? 2
  • +

Replies To: Secure File Deletion in VB.NET

#2 Guest_Z*


Reputation:

Posted 09 September 2010 - 07:45 AM

I get the error "The value of argument 'showUI' (0) is invalid for Enum type 'UIOption'.
Parameter name: showUI

Although it does fill the file with 1 and 0's
Was This Post Helpful? 0

#3 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Posted 13 September 2010 - 09:04 AM

see line 29 of btnDelete, won't writeline(all) add a new line for every pass?
Was This Post Helpful? 0
  • +
  • -

#4 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Posted 14 September 2010 - 02:01 AM

Whoops. You're right. It should just be Write(All).
Was This Post Helpful? 0
  • +
  • -

#5 MistGun  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 27-October 10

Posted 28 October 2010 - 02:41 PM

This is pretty nice, I'll give it a try and come back later for feedback. Thanks for sharing!
Was This Post Helpful? 0
  • +
  • -

#6 cfpcompte  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-November 10

Posted 01 November 2010 - 11:26 AM

Just a quick note: This may not work at all. There is a high probability that the system won't write your array at the same physical location that the original file. To reduce fragmentation, or for any other reason, it may well decide to move it somewhere else. In many cases, you won't be overwriting the file at all.
Plus, if the drive is compressed, you array will most likely compress to much less than the original file, and thus, even if the system did write at the same location, the tail of the original file will most likely not be affected.

In most cases, therefore, you won't need hardware tools at all to recover the files deleted with this method; software tools will succeed easily. You can convince yourself of this fact by just testing this process on a few files. It doesn't work well at all.

CFP.
Was This Post Helpful? 0
  • +
  • -

#7 TackleBox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-November 10

Posted 20 November 2010 - 10:31 AM

This program compiled fine and seems to run although it never really deletes the file. Oh and it frezzes at the end of the run after I click Delete. I get no Errors and debugging didn't seem to show anything ether.
Here is what I had going....

Public Class Form1
Dim ofd As New OpenFileDialog

Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
'Dim ofd As New OpenFileDialog 'I dont know if it should be here or up top both seem to work the same

'Make the title say "Choose File to Delete"
ofd.Title = "Choose File to Delete"

'make sure thet the user can choose any file
ofd.Filter = "All Files |*.*"

'show the OpenFileDialog, and if use presses OK,
'then set the text of the file path to the path of the selected file
If ofd.ShowDialog = DialogResult.OK Then
txtFilePath.Text = ofd.FileName
End If

End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'...
If My.Computer.FileSystem.FileExists(txtFilePath.Text) Then
'..
Dim passes As Integer = 0
'..
Try
'in this...
Do Until passes = 10
passes += 1
'..
Dim array = IO.File.ReadAllBytes(txtFilePath.Text)
'messy
Dim OneOrZero As Integer = 0
Dim all As String = String.Empty
'for each
For Each selbyte In array
'change 1 or 0
If OneOrZero = 0 Then
selbyte = "0"
OneOrZero = 1
ElseIf OneOrZero = 1 Then
selbyte = "1"
OneOrZero = 0
End If
all &= selbyte
Next
'write...
Dim SaveF As New IO.StreamWriter(txtFilePath.Text)
SaveF.Write(all)
SaveF.Close()
'loop 10 times
Loop
'delete
My.Computer.FileSystem.DeleteFile(txtFilePath.Text, False, False)
Catch ex As Exception
'If We encounter
MessageBox.Show("ERROR:" & ex.Message, "Error")
End Try
Else
'if.....
MessageBox.Show("error:check name", "Error")
End If
End Sub
End Class

I'm a complete noob so if anyone can help me see what I did wrong I feel it would be a good learning exspeeriance for me.
Thank you all.
Was This Post Helpful? 0
  • +
  • -

#8 Zher0k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-July 11

Posted 30 July 2011 - 09:45 PM

Hmm will try this out and post back, looks great.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1