sakya
asked on
Delete a file
Hello.
I want to program to delete a file that can't undelete again like a shredder machine.
Please teach me a code or a website where I can download such a component.
Sakya
I want to program to delete a file that can't undelete again like a shredder machine.
Please teach me a code or a website where I can download such a component.
Sakya
Here is an example.////
DeleteFile('C:\windows\des ktop\somet hing.txt') ;
Regards,
Viktor Ivanov
DeleteFile('C:\windows\des
Regards,
Viktor Ivanov
All deleted file may be recovered if you know how to crack it. so the question is how secure do you want it to be ?
Hi,
This is not an easy question.
To be able to "shred" a file in order to not be able to read it afterwards is a tough one. DeleteFile will only remove/hide the allocation table entry and not really shred/wipe the data.
To wipe a file, you will have to replace it's sectors data with patterns or pseudorandom data.
Here's an example on how to do it. Do NOT trust this code, since it is not properly tested. The problems are:
1. If you modify the file, does the OS move the data physically so that the old data resides, unchanged, in it's old position?
2. If you shred the data and delete the file afterwards, do you know if the new data is physically changed on the disk or are the changes just made in the cache?
Anyway, here's something that MIGHT work. You can select pattern or pseudorandom shredding and number of passes:
function ShredFile( sFilename: string; cPasses: Integer; bPR, bDelete: Boolean ): Boolean;
type
PBuf = ^TBuf;
TBuf = array[ 0..0 ] of Byte;
const
BLOCKSIZE = 65536;
PATTERN: array[0..3] of Byte = ( $A, $A, $5, $5 );
var
fs : TFileStream;
iIndex,
iBytesToWrite,
iBytesWritten,
iBufSize : Integer;
p : PBuf;
begin
Result := False;
p := nil;
try
if FileExists( sFilename ) then begin
fs := TFileStream.Create( sFilename, fmOpenReadWrite or fmShareExclusive );
if fs.Size < BLOCKSIZE
then iBufSize := fs.Size
else iBufSize := BLOCKSIZE;
try
GetMem( p, iBufSize );
if not bPR then begin
for iIndex := 0 to iBufSize - 1 do begin
p^[ iIndex ] := PATTERN[ iIndex and 3 ];
end;
end else Randomize; // initiate pseudorandom series
while cPasses > 0 do begin
fs.Seek( 0, soFromBeginning ); // always from the beginning
repeat
if bPR then for iIndex := 0 to iBufSize - 1 do p^[ iIndex ] := Random( 255 );
if ( fs.Size - fs.Position ) <= BLOCKSIZE
then iBytesToWrite := fs.Size - fs.Position
else iBytesToWrite := BLOCKSIZE;
iBytesWritten := fs.Write( p^, iBytesToWrite ); // speeds up things
until ( iBytesToWrite < BLOCKSIZE ) or ( iBytesWritten <> iBytesToWrite );
if iBytesWritten <> iBytesToWrite then begin
Result := False;
raise Exception.Create( 'Unable to write sufficient #bytes to file' );
end;
Dec( cPasses );
end;
fs.Free;
if bDelete then Result := DeleteFile( sFilename );
finally
FreeMem( p );
fs.Free;
end;
end;
except end;
end;
Usage:
ShredFile( 'c:\mysecret.dat', 255, True, False ); // 255 passes, pseudorandom, keep file
ShredFile( 'c:\mysecret.dat', 1, False, True ); // 1 pass, pattern, delete file
ShredFile( 'c:\mysecret.dat', 0, False, True ); // just delete file
/// John
This is not an easy question.
To be able to "shred" a file in order to not be able to read it afterwards is a tough one. DeleteFile will only remove/hide the allocation table entry and not really shred/wipe the data.
To wipe a file, you will have to replace it's sectors data with patterns or pseudorandom data.
Here's an example on how to do it. Do NOT trust this code, since it is not properly tested. The problems are:
1. If you modify the file, does the OS move the data physically so that the old data resides, unchanged, in it's old position?
2. If you shred the data and delete the file afterwards, do you know if the new data is physically changed on the disk or are the changes just made in the cache?
Anyway, here's something that MIGHT work. You can select pattern or pseudorandom shredding and number of passes:
function ShredFile( sFilename: string; cPasses: Integer; bPR, bDelete: Boolean ): Boolean;
type
PBuf = ^TBuf;
TBuf = array[ 0..0 ] of Byte;
const
BLOCKSIZE = 65536;
PATTERN: array[0..3] of Byte = ( $A, $A, $5, $5 );
var
fs : TFileStream;
iIndex,
iBytesToWrite,
iBytesWritten,
iBufSize : Integer;
p : PBuf;
begin
Result := False;
p := nil;
try
if FileExists( sFilename ) then begin
fs := TFileStream.Create( sFilename, fmOpenReadWrite or fmShareExclusive );
if fs.Size < BLOCKSIZE
then iBufSize := fs.Size
else iBufSize := BLOCKSIZE;
try
GetMem( p, iBufSize );
if not bPR then begin
for iIndex := 0 to iBufSize - 1 do begin
p^[ iIndex ] := PATTERN[ iIndex and 3 ];
end;
end else Randomize; // initiate pseudorandom series
while cPasses > 0 do begin
fs.Seek( 0, soFromBeginning ); // always from the beginning
repeat
if bPR then for iIndex := 0 to iBufSize - 1 do p^[ iIndex ] := Random( 255 );
if ( fs.Size - fs.Position ) <= BLOCKSIZE
then iBytesToWrite := fs.Size - fs.Position
else iBytesToWrite := BLOCKSIZE;
iBytesWritten := fs.Write( p^, iBytesToWrite ); // speeds up things
until ( iBytesToWrite < BLOCKSIZE ) or ( iBytesWritten <> iBytesToWrite );
if iBytesWritten <> iBytesToWrite then begin
Result := False;
raise Exception.Create( 'Unable to write sufficient #bytes to file' );
end;
Dec( cPasses );
end;
fs.Free;
if bDelete then Result := DeleteFile( sFilename );
finally
FreeMem( p );
fs.Free;
end;
end;
except end;
end;
Usage:
ShredFile( 'c:\mysecret.dat', 255, True, False ); // 255 passes, pseudorandom, keep file
ShredFile( 'c:\mysecret.dat', 1, False, True ); // 1 pass, pattern, delete file
ShredFile( 'c:\mysecret.dat', 0, False, True ); // just delete file
/// John
ASKER
Thanks.
Yes. This is security problem.
erojoj is right.
'DeleteFile' just deletes the allocation table.
If it's a text type file, it will be restored.
Now I am testing erojoj's code.
Sakya
Sakya
Yes. This is security problem.
erojoj is right.
'DeleteFile' just deletes the allocation table.
If it's a text type file, it will be restored.
Now I am testing erojoj's code.
Sakya
Sakya
ASKER
Hello.
The erojoj's code works on windows95.
But I don't test it on OSR2 or NT.
I've heard Shredder code is different between win95 ver.1 and OSR2 before.
Anyway thanks.
I want to give my points to erojoj's excellent code.
Sakya
The erojoj's code works on windows95.
But I don't test it on OSR2 or NT.
I've heard Shredder code is different between win95 ver.1 and OSR2 before.
Anyway thanks.
I want to give my points to erojoj's excellent code.
Sakya
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
Sakya
Sakya
Thanks Motaz.
My comments fade to oblivion in the shine of your excellent answers.
/// John
My comments fade to oblivion in the shine of your excellent answers.
/// John
Thanks John
Motaz.
Motaz.
ASKER