I have wanted to create a short piece of code for a while which allows me to send files over work email. I know logically what I want to do but I've always had issues with VB6 type declarations and manipulating the types when working in binary.
I noticed that AV scanners tended to determine the file type by the beginning and end of a file. Especially executable files. If you scramble the file it cannot execute and that's all AV scanners are interested in.
So I told ChatGPT exactly what I wanted it to do.
Create code in VB6 to 1) open a file, given in the command line parameters as file name as binary for read write 2) read the first 50 bytes to an array of type byte called _first 3) read the last 100 bytes into an array as type byte called _Last 4) exchange the first 50 bytes from _first with each alternate byte in _Last. Byte 1 of _First will be exchanged with byte 2 of _Last. 4) Write back _First to the first 50 bytes of the file 5) write back _Last to the last 100 bytes of the file.
It took about 75 seconds and below is what it produced. It looks pretty solid. I'll have to compile and test. Then I'll have to test it with the work email. This is going to be interesting. We have had to jump through so many hoops to exchange program files with each other. Some work email systems will not allow encrypted compressed files to be exchanged. They demand the password first so they can scan what is in it.
I'm hoping this will work. Clearly I could exchange 50 and 100 with 1,000 and 2,000 if the signature is still there.
ChatGPT is fast becoming my favourite toy. I may have to ask it something really difficult like using transformation of data by multiple formula to scramble it up and try recursive compression. That might really be fun. Because it is no fun at all to think up the code.
Option Explicit
Private Type ByteArray
Data() As Byte
End Type
Private _file As Integer
Private _first As ByteArray
Private _last As ByteArray
Private Sub Main()
' Get the file name from the command line parameters
Dim fileName As String
fileName = Command$
' Open the file in binary mode for read/write
_file = FreeFile
Open fileName For Binary Access Read Write As #_file
' Read the first 50 bytes into _first
ReDim _first.Data(0 To 49) As Byte
Get #_file, , _first.Data
' Read the last 100 bytes into _last
Seek #_file, LOF(file) - 100
ReDim _last.Data(0 To 99) As Byte
Get #_file, , _last.Data
' Exchange the first 50 bytes from _first with each alternate byte in _last
Dim i As Integer
For i = 0 To 49
Dim temp As Byte
temp = _first.Data(i)
_first.Data(i) = _last.Data(i * 2 + 1)
_last.Data(i * 2 + 1) = temp
Next i
' Write back _first to the first 50 bytes of the file
Seek #_file, 1
Put #_file, , _first.Data
' Write back _last to the last 100 bytes of the file
Seek #_file, LOF(file) - 100
Put #_file, , _last.Data
' Close the file
Close #_file
End Sub