How to replace or remove last 500 bytes of a file without rewriting all the file?

  • Thread starter Thread starter G-A-F-S
  • Start date Start date
G

G-A-F-S

Guest
Hi everyone,

Usually I only ask for help when I cant find a solution for several days or weeks... And guess what? That just happen!

So, this is what i am trying to do:

I have a program to ZIP folder and protect them with password, then it encrypts the zip file.

That its working fine, until the user forgets his password.

So, what I want to do is give the user a Recovery Password option for each ZIP file created. I cant use the Windows Registry because the idea is to be able to recover the password in any computer.So i came up with an idea...

In simple terms, this will work like this:

0 - Choose folder to ZIP

1 - Ask user for recover details (date of birth, email etc)

2 - ZIP folder with password

3 - Encrypt ZIP file

4 - Encrypt recover details and convert it to HEX

5 - Add recover details (in HEX) to the end of the ZIP file (last bytes)

6 - Add "5265636F76657244657461696C73" which is the text "RecoverDetails" in HEX

7 - Add "504B0506000000000000000000000000000000000000" this is the final bytes of a ZIP file and will make the Operating System think that is a ZIP file (i know that will give an error when we try to open it.. the ideia is to change the extension later and use my software to do all the work to access this ZIP/folder again)

So, explaining what its here, I want to say that I managed how to do all of this so far. The point number 6 will help us to determine where the recover details are in the file, or if they actually exist because user can choose not to use them.



In order to unlock this ZIP and extract its contents, I need to reverse what Ive done. That means, that need to read only the last 500 bytes (or less if the file is smaller) of the ZIP and remove those extra bytes I added so the program can check if the user is inputing a correct password, and if so decrypt contents and extract them.

But, if the user insert a wrong password I need to re-add those bytes with the recover details again to the ZIP file.



The second thing is, if the user forgets his password and asks to recover it, a form will be shown asking to insert the recover detail (date of birth, email etc), so we need to reed the last 500 bytes of the ZIP, find the bytes in number 6 and remove the bytes before number 6, remove bytes in number 6 and number 7, and we will have the recover details to match against the user details input.

I have all done so far with the locking process. But i need help with the unlocking.

I am not sure if its possible, but this what i am looking for:

Read last 500 bytes of a file, remove the bytes with recover details and save the file. Without reading the whole file, because if we have a 1GB file that will take a very long time. Also, i dont want to "waste" hard drive space creating a new clone file with 1GB and then delete the original.

And then add them back "in case user fails the password" which should be exactly the same.

This sounds a bit confusing I know, even to me, I am writing and trying to explain this the better I can.. Also my English is not the best..

Here it goes some code to better understanding:


READ LAST 500 BYTES OF ZIP FILE TO CHECK IF IT CONTAINS RECOVER DETAILS

Dim oFileStream As New FileStream(TextBox_ZIP_to_Protect.Text & ".zip", FileMode.Open, FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 500
Dim fileData As Byte() = oBinaryReader.ReadBytes(500)
oBinaryReader.Close()
oFileStream.Close()

Dim txtTemp As New System.Text.StringBuilder()
For Each myByte As Byte In fileData
txtTemp.Append(myByte.ToString("X2"))
Next

Dim RecoveryDetailsPass_Holder = txtTemp.ToString()

Dim Temp_2 = txtTemp.ToString()


RichTextBox1.Text = txtTemp.ToString()

If txtTemp.ToString.Contains("505245434F47414653") Then
we have password recovery details(the numbers mean RecoverDetails in HEX)

next we will get rid of everything before and after of string "cut_at"

Dim mystr As String = RecoveryDetailsPass_Holder RichTextBox1.Text
Dim cut_at As String = "505245434F47414653"
Dim x As Integer = InStr(mystr, cut_at)

Dim string_before As String = mystr.Substring(0, x - 1)
Dim string_after As String = mystr.Substring(x + cut_at.Length - 1)


RecoveryDetailsPass_Holder = RecoveryDetailsPass_Holder.Replace(string_after.ToString, "")

RecoveryDetailsPass_Holder = RecoveryDetailsPass_Holder.Replace("505245434F47414653", "") this is RecoverDetails in HEX

RecoveryDetailsPass_Holder = RecoveryDetailsPass_Holder.Replace("504B0506000000000000000000000000000000000000", "") this is the bytes of an empty zip file

AT THIS POINT WE HAVE ONLY THE RECOVER PASSWORD DETAILS (date of birth, email etc) IN THE VARIABLE "RecoveryDetailsPass_Holder"


////////////////////////////////////////////////////// TO DEBUG
MsgBox(string_after.ToString & "505245434F47414653")
InputBox("", "", string_after.ToString)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ TO DEBUG


Temp_2 = Temp_2.Replace(RecoveryDetailsPass_Holder.ToString, "")


Now that we have the recover details, we need to remove them from ZIP in order to the software try to unzip it with the password provided by the user on the GUI.

If the user needs to recover the password we have the details already in RecoveryDetailsPass_Holder variable and just need to match them against user input details.

If the user fails, we need to put the RecoveryDetailsPass_Holder back on the file.

Any question just ask, its a bit trick to explain i think, but please ask.

Anyone know how to do this?

Many thanks in advanced.




Nothing is impossible!
@ Portugal
-----
Vote if its helpfull :)

Continue reading...
 
Back
Top