Hii

I have been converting a file into its hex value.But cannot convert it back into same exe value

Let me get in detail

Here is the code

Imports System.IO
Sub Main()

 

        Dim fileName As String = "ABC.exe"

 

        Dim data As Byte() = File.ReadAllBytes(fileName)

 

        Dim fs As FileStream = File.OpenWrite(fileName & ".txt")

        Dim sw As StreamWriter = New StreamWriter(fs)

 

        Dim pos As Integer = 0

        Dim line As Integer = 0

 

        While pos < data.Length

            sw.Write(String.Format("{0}:", line.ToString("0000")))

 

            For i As Integer = 1 To 20

                sw.Write(String.Format(" {0:x2}", data(pos)))

                pos += 1

                If pos = data.Length Then

                    Exit For

                End If

            Next

 

            sw.WriteLine()

            line += 1

        End While

 

        sw.Flush()

        sw.Close()

        fs.Close()

    End Sub

Now,When i call the main method an text file called ABC.exe.txt is generated.

First of all instead of ABC.exe.txt can we have ABC.txt only

and secondly can how can i convert this hex values back to exe.. at least compile it in vb or any other language

Thank you for your time

Recommended Answers

All 11 Replies

I made this differently than your original approach.
Please consider this code where you supply both the input name and the output name.
This helps to prevent unintentionally overwriting files.

You can still morph the input file name (from .exe to .txt), if you wish before calling the functions.

I ran this over an EXE I created called Today.exe and it works in both directions (and the end result is exactly the same).

I compared the original exe with the final exe with the command:
fb /b c:\bin\Today.exe c:\science\NewToday.exe
(no differences encountered)

Imports System.IO
Module mod378258
   Private Sub Encode(ByVal strFrom As String, ByVal strTo As String)
      Dim fileIn As New StreamReader(strFrom)
      Dim fileOut As New StreamWriter(strTo)
      Dim i As Integer = 0

      For i = 1 To fileIn.BaseStream.Length

         fileOut.Write(Convert.ToString(fileIn.BaseStream.ReadByte(), 16).PadLeft(2, "0"))
         If 0.Equals((i Mod 16)) Then
            fileOut.WriteLine()
         End If
      Next i

      fileOut.Close()
      fileIn.Close()
   End Sub
   Private Sub Decode(ByVal strFrom As String, ByVal strTo As String)
      Dim fileIn As New StreamReader(strFrom)
      Dim fileOut As New StreamWriter(strTo)
      Dim strData As String = ""
      Dim i As Integer = 0

      While (Not (fileIn.EndOfStream))
         strData = fileIn.ReadLine()
         For i = 0 To (strData.Length - 1) Step 2
            fileOut.BaseStream.WriteByte(Convert.ToUInt16(strData.Substring(i, 2), 16))
         Next
      End While
         
      fileOut.Close()
      fileIn.Close()
   End Sub

   Sub Main()
      Encode("c:/bin/Today.exe", "c:/science/Today.txt")
      Decode("c:/science/Today.txt", "c:/science/NewToday.exe")
   End Sub

End Module

Ya thanks a lot it was very helpful...NOw i need only one thing..When the file is decoded to exe the code takes those binary value from text and convert it..Is there any way i can edit the elements of text while conversion????...I mean before the file is converted back to exe there is a process where those binary values are being converted now i want to edit those values is it possible???

Yes. At this point, they are simply 80x86 op-codes.
They represent assembly language commands and data values.
If you know what they are, you can add/change/delete them.

Ok I am looking for something like hex editor..where you can edit those values.this should make my idea clear

ok...here is the thing when "Today.txt" is generated I want to take that content in an array and then write the content of an array on a new txt file....IS that possible???

OK here is the thing when the file "Today.txt" is generated i want to take the contents of the file in an array...how can i do it??

Yes, when reading the file, add each byte to the end of an array.

There is also a method of reading the entire file at once into an array:

Dim arr_allBytes As Byte() = File.ReadAllBytes("c:/bin/Today.exe")

So, what you were really asking is:
How does one make a HEX Editor in VB.NET and test it on an EXE file?

Right?

My earlier code post in this thread, shows how to take the EXE, convert it to a text file and convert that back to an EXE file.

If you want, you can modify the .TXT file between steps (either using a text editor or your code) so that when the .EXE is re-created, it will have new values.

There is a chance the new EXE will not run properly depending on any values that are changed.

My earlier code post in this thread, shows how to take the EXE, convert it to a text file and convert that back to an EXE file.

If you want, you can modify the .TXT file between steps (either using a text editor or your code) so that when the .EXE is re-created, it will have new values.

There is a chance the new EXE will not run properly depending on any values that are changed.

Well the file may or may not run if the content is modified or replaced...But if any content is deleted the file would definitely wont run.Because in an exe file every element has a fixed offset and if the element at the offset is missing the file wont run.So only replacing or modifying is the option....

So, is there anything left to do with this project?
Is it finished?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.