Jump to content

Reading file as hex, saving it as hex.


Recommended Posts

Hi guys, I need help.

I currently have this code:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <String.au3>
Opt('MustDeclareVars', 1)
Local $btn1, $msg, $btn2, $btn3, $btn4, $btn5, $var, $xOrigText, $xEncryptText, $file, $xxEncryptTex
Local $sOrig = '470123" & @CR & @LF
Local $sConv = " #$%&''& @CR & @LF
Global $aOrig = StringSplit($sOrig, "", 2)
Global $aConv = StringSplit($sConv, "", 2)
MsgB()
;Local $sOrigText = "" & "" & "" & ""
GUICreate("Majid's Khan .BIN Cryptor v1.0.3", 460, 570); WS_EX_ACCEPTFILES , -1, -1, -1, 0x00000018
$xOrigText = GUICtrlCreateEdit("Enter decrypted text here." & @TAB & "You cant use the Alt+A key but you may rightclick then select all.", 1, 1, 459, 250)
$xEncryptText = GUICtrlCreateEdit("Enter encrypted text here." & @TAB & "Khan .BIN files are found at /data/msg/*.bin or some other folders.", 1, 300, 459, 250)
$btn5 = GUICtrlCreateButton("Encrypt", 40, 260, 80, 30)
$btn4 = GUICtrlCreateButton("Decrypt", 120, 260, 80, 30)
$btn3 = GUICtrlCreateButton("Load Encrypted File", 200, 260, 110, 30)
$btn2 = GUICtrlCreateButton("Save", 310, 260, 60, 30)
$btn1 = GUICtrlCreateButton("Exit", 370, 260, 50, 30)
GUICtrlCreateLabel("Created by Majid Siddiqui for khan.forumotion.com", 130, 553)
GUISetState()
$msg = 0
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $btn1, $GUI_EVENT_CLOSE
            ExitLoop
  Case $btn2
   $file = FileOpen($var, 1)
   $xxEncryptTex = _HexToString("1d")
   FileWrite($file, $xxEncryptTex)
   FileClose($file)
  Case $btn5
            If GUICtrlRead($xOrigText) <> "" Then _
                    GUICtrlSetData($xEncryptText, StringRegExpReplace(_Encrypt(GUICtrlRead($xOrigText)), _
     "(\d+)(.+\v*)", "\1" & @TAB & "\2"))
        Case $btn4
            If GUICtrlRead($xEncryptText) <> "" Then _
                    GUICtrlSetData($xOrigText, StringRegExpReplace(_Decrypt(GUICtrlRead($xEncryptText)), _
                    "(\d+)(.+\v*)", "\1" & @TAB & "\2"))
        Case $btn3
            $var = FileOpenDialog("Locate Khan .Bin File.", @ScriptDir & "\", _
                    "Bin files (*.bin)|Text files (*.txt)|All (*.*)", 1 + 4)
            If Not @error Then
                GUICtrlSetData($xEncryptText, FileRead($var))
   EndIf
    EndSwitch
WEnd
 
Func _Encrypt($sOrigText)
    Local $iIndex, $sConvText = ""
    Local $aOrigText = StringSplit($sOrigText, "", 2)
    For $i = 0 To UBound($aOrigText) - 1
        $iIndex = _ArraySearch($aOrig, $aOrigText[$i], 0, 0, 1) ; Case sensitive search
        If $iIndex <> -1 Then $sConvText &= $aConv[$iIndex]
    Next
    Return $sConvText
EndFunc ;==>_Encrypt
Func _Decrypt($sConvText)
    Local $iIndex, $sOrigText = ""
    Local $aConvText = StringSplit($sConvText, "", 2)
    For $i = 0 To UBound($aConvText) - 1
        $iIndex = _ArraySearch($aConv, $aConvText[$i], 0, 0, 1) ; Case sensitive search
        If $iIndex <> -1 Then $sOrigText &= $aOrig[$iIndex]
    Next
    Return $sOrigText
EndFunc ;==>_Decrypt
Func MsgB()
MsgBox(4096, "Welcome | Notice", "This program will help you view encrypted files, and create translations for Khan.", 0)
EndFunc   ;==>Menu

But, I this is how I want it.

I want to read file as hex, and save it as hex.. But when I open the file via text editor it will still display the anscii/string.

As If my script was an hex editor.

My reason, as you see my script is a conversion script. the problem is I can't convert "TAB" to the correct value because its [GS], but in hex its 1D. So, to implement it I wish to read where a [GS] is as HEX then view it as a TAB. When saving I just wan't the file to be encrypted again.

Thanks, God Bless.

Edited by madsiddiqui
Link to comment
Share on other sites

Anyways this is the conversion data.

A file contains something like this,

$[THIS IS A TAB]Z{4Bqfg}{z4]zr{:4@|}g4df{sfuy4c}xx4vq4wx{gqp:

converts to

0[THIS IS A TAB]No Version Info. This program will be closed.

I have this working, except for the tab. In string the conversion is 4 = [space], $ = 0..so on..

But I wish to read the file's hex data, and convert it.

Conversion table,(decryption)

HEX -> STRING

If hex 20 is found convert it to hex(4).

If hex 21 is found convert it to hex(5).

If hex 1e is found convert it to newline.

If hex 1d is found convert it to TAB.

Conversion table,(encryption)

HEX -> HEX

If hex(4) is found convert to 20

If hex(5) is found convert to 21

If newline is found convert to 1e

If TAB is found convert to 1d

But when I view the file from notepad++ It should still be the string data showing..

Edited by madsiddiqui
Link to comment
Share on other sites

Look at the flags in FileOpen() & then _StringToHex() and _HexToString(). Hopes this gets you started.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I now have this code,

But the problem is its still not reading the HEX of the file, but its just reading the file then converting it to hex... :mellow:

Please advise?

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <String.au3>
;declerations here
Local $sOrig = '!"' & "#$%&'()*+,-./01234 56789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ" & _HexToString("1d") & _HexToString("1e")
Local $sConv = "5670123<=>?89:;$%&'" & ' 4!"#,-./()*+TUVWPQRS\]^_XYZ[DEFG@ABCLMNOHIJKtuvwpqrs|}~xyz{defg`abclmnohijk”•–—‘’“œžŸ˜™š›„…†‡€‚ƒŒŽˆ‰Š‹´µ¶·°±²³¼½¾¿¸¹º»¤¥¦§ ¡¢£¬­®¯¨©ª«ÔÕÖ×ÐÑÒÓÜÝÞßØÙÚÛÄÅÆÇÀÁÂÃÌÍÎÏÈÉÊËôõö÷ðñòóüýþÿøùúûäåæçàáâãìíîïèéê' & '@TAB' & '@CRLF'
Global $aOrig = StringSplit($sOrig, "", 1)
Global $aConv = StringSplit($sConv, "", 1)

MsgB()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("BlazeKhan BIN Packer/Unpacker Tool", 571, 446, 192, 124)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 553, 353, BitOR($GUI_SS_DEFAULT_EDIT,$ES_OEMCONVERT))
$Open = GUICtrlCreateButton("Open", 16, 384, 89, 33)
$Unpack = GUICtrlCreateButton("Unpack", 112, 384, 89, 33)
$Repack = GUICtrlCreateButton("Repack", 208, 384, 89, 33)
$Save = GUICtrlCreateButton("Save", 304, 384, 89, 33)
$Exit = GUICtrlCreateButton("Exit", 496, 384, 57, 33)
$Reset = GUICtrlCreateButton("Reset", 400, 384, 89, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUISetState()
$msg = 0
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $Exit, $GUI_EVENT_CLOSE
            ExitLoop
  Case $Reset
   GUICtrlSetData($Edit1, "")
  Case $Unpack
            If GUICtrlRead($Edit1) <> "" Then _
                    GUICtrlSetData($Edit1, _Encrypt(GUICtrlRead($Edit1)))
        Case $Repack
            If GUICtrlRead($Edit1) <> "" Then _
                    GUICtrlSetData($Edit1, _Decrypt(GUICtrlRead($Edit1)))
        Case $Open
            $var = FileOpenDialog("Locate Khan .Bin File.", @ScriptDir & "\", _
                    "Bin files (*.bin)|Text files (*.txt)|All (*.*)", 1 + 4)
            If Not @error Then
                GUICtrlSetData($Edit1, FileRead($var))
   EndIf
  Case $Save
   $hFile = FileOpen($var, 17)
   $hFile2 = FileOpen($var, 17)
   $chars2 = FileRead($hFile2)
   $bString2 = StringMid(_HexToString(GUICtrlRead($Edit1)), 3)
   FileWrite($hFile2, $bString2)
   FileClose($hFile2)
    EndSwitch
WEnd

;functions here
Func _Encrypt($sOrigText)
    Local $iIndex, $sConvText = ""
    Local $aOrigText = StringSplit($sOrigText, "", 2)
    For $i = 0 To UBound($aOrigText) - 1
        $iIndex = _ArraySearch($aOrig, $aOrigText[$i], 0, 0, 1) ; Case sensitive search
        If $iIndex <> -1 Then $sConvText &= $aConv[$iIndex]
    Next
    Return $sConvText
EndFunc ;==>_Encrypt
Func _Decrypt($sConvText)
    Local $iIndex, $sOrigText = ""
    Local $aConvText = StringSplit($sConvText, "", 2)
    For $i = 0 To UBound($aConvText) - 1
        $iIndex = _ArraySearch($aConv, $aConvText[$i], 0, 0, 1) ; Case sensitive search
        If $iIndex <> -1 Then $sOrigText &= $aOrig[$iIndex]
    Next
    Return $sOrigText
EndFunc ;==>_Decrypt
Func MsgB()
MsgBox(4096, "Welcome | Notice", "This program will help you view encrypted files, and create translations for Khan.", 0)
EndFunc   ;==>Menu
Link to comment
Share on other sites

  • Moderators

But the problem is its still not reading the HEX of the file, but its just reading the file then converting it to hex

This makes absolutely no sense.

What is the "Hex" of a file?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...