I’ve brought a binary stream into a Memory Block, Encrypted it then used:
bs.Write(mb.StringValue(0, mb.Size))
This adds the Encrypted version to the end of the file which is not what I’m attempting to do. I’m obviously missing something
Do I need to delete the original file?
Do I need to close the binary stream then reopen it?
Do I need to use bs.Position = 0?
D) none of the above.
TIA
Hi Kem,
So to understand that properly; (simple example)
File is 10 bytes, the Memory Block is 8 bytes
Setting the Length = 0 then using Write will end up with a File that is now 8 bytes? deleting the 2 extra bytes in the file. OR does Length = 0 delete all the data immediately ?
I’m not sure I see the difference in your alternatives as you will end up with a file of 8 bytes either way. If you just want to erase the contents, set the Length to 0 and close the BinaryStream immediately.
Just remember that “erase” does not mean “overwrite” where the OS is concerned. The original data in your file will probably be somewhere on the disk for someone determined to find it. That’s true in all cases.
If you want to mitigate that, I suppose you could read the file, set the Position to 0, write back an equal length of gibberish, then close the BinaryStream. Then open the file again and write over it. But I doubt that’s guaranteed either. It’s one of the reasons Apple got rid of their “Secure Erase” option.
Thanks, the encrypted is always at least the same length (or 31 bytes longer) than the original and is ‘gibberish’ so it essentially does the same job as the above?
Yes. It truncates the file immediately. Setting the length larger writes null bytes (0x00) to the file. So you can quickly erase the bytes in the file with
If the file is deleted and something goes wrong, e.g. power cut, then the data is lost, so it may be better to put it in the trash or rename it then after the new file is created then delete it.
@Tim Hare @Kem Tekinay
Just playing with Text files this works so I’m wondering if this also an answer?
test.txt = “This is my test file”
[code] Dim f As FolderItem = GetFolderItem("…\test.txt")
Dim bs As BinaryStream = BinaryStream.Open(f, True)
Dim s As String = bs.Read(bs.Length)
bs.Length = 5
bs.Position = 0
bs.Write(“Hello”)