Page 1 of 1

Tool Create DataSections with AES Strings

Posted: Sun Dec 31, 2023 5:05 pm
by mk-soft
Was once again a request (German) how to protect strings in the executable
Here is a tool to create a DataSection with AES encrypted strings.

Update v1.03

Code: Select all

;-TOP by mk-soft, v1.03, 31.12.2023, 01.01.2024

; Create AES DataSection Strings

Macro AddElementValue(_list_, _value_)
  AddElement(_list_) : _list_ = _value_
EndMacro

Structure ArrayOfByte
  b.b[0]
EndStructure

Procedure CreateAESDataSection(Filename.s)
  Protected label.s, text.s, len, size, *buffer.ArrayOfByte, i, temp.s
  Protected NewList rows.s()
  
  Restore Strings
  
  AddElementValue(rows(), ";-TOP AES DataSection Strings")
  AddElementValue(rows(), "")
  AddElementValue(rows(), "Procedure.s GetAESDataString(*Label)")
  AddElementValue(rows(), "  Protected len, size, temp.s")
  AddElementValue(rows(), "  len = PeekL(*Label)")
  AddElementValue(rows(), "  If len < 8")
  AddElementValue(rows(), "    size = 16")
  AddElementValue(rows(), "  Else")
  AddElementValue(rows(), "    size = len * 2")
  AddElementValue(rows(), "  EndIf")
  AddElementValue(rows(), "  *Label + 4")
  AddElementValue(rows(), "  temp = Space(size / 2)")
  AddElementValue(rows(), "  If AESDecoder(*Label, @temp, size, ?StringKey, 128, ?StringInitializationVector)")
  AddElementValue(rows(), "    temp = Left(temp, len)")
  AddElementValue(rows(), "  EndIf")
  AddElementValue(rows(), "  ProcedureReturn temp")
  AddElementValue(rows(), "EndProcedure")
  AddElementValue(rows(), "")
  AddElementValue(rows(), "DataSection")
  AddElementValue(rows(), "  StringKey:")
  *buffer = ?StringKey
  For i = 0 To 15
    If i = 0
      temp = "  Data.b $" + RSet(Hex(*buffer\b[i], #PB_Byte), 2, "0")
    Else
      temp + ",$" + RSet(Hex(*buffer\b[i], #PB_Byte), 2, "0")
    EndIf
  Next
  AddElementValue(rows(), temp)
  AddElementValue(rows(), "  StringInitializationVector:")
  *buffer = ?StringInitializationVector
  For i = 0 To 15
    If i = 0
      temp = "  Data.b $" + RSet(Hex(*buffer\b[i], #PB_Byte), 2, "0")
    Else
      temp + ",$" + RSet(Hex(*buffer\b[i], #PB_Byte), 2, "0")
    EndIf
  Next
  AddElementValue(rows(), temp)
  AddElementValue(rows(), "")
  
  ; Max String Len 32768
  *buffer = AllocateMemory($10000)
  
  Repeat
    Read.s label
    If label = #ETX$
      Break
    EndIf
    Read.s text
    len = Len(text)
    If len < 8
      text + Space(8 - len)
    EndIf
    AddElementValue(rows(), "  " + label)
    size = StringByteLength(text)
    AddElementValue(rows(), "  Data.l " + Str(len))
    If AESEncoder(@text, *buffer, size, ?StringKey, 128, ?StringInitializationVector)
      For i = 0 To size - 1
        If i = 0
          temp = "  Data.b $" + RSet(Hex(*buffer\b[i], #PB_Byte), 2, "0")
        Else
          temp + ",$" + RSet(Hex(*buffer\b[i], #PB_Byte), 2, "0")
        EndIf
      Next
      AddElementValue(rows(), temp)
    EndIf
    
  ForEver
  AddElementValue(rows(), "EndDataSection")
  AddElementValue(rows(), "")
  
  If Filename
    If CreateFile(0, Filename)
      ForEach rows()
        WriteStringN(0, rows())
      Next
      CloseFile(0)
    EndIf
  Else
    ForEach rows()
      Debug rows()
    Next
  EndIf
  
  FreeMemory(*buffer)
  
EndProcedure

CreateAESDataSection("")

;- Data
; Strings:
; - Label name
; - Text contents
; ...
; ETX

DataSection
  StringKey:
  Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $07
  StringInitializationVector:
  Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $42
  
  Strings:
  Data.s "text1:"
  Data.s "Hello World!"
  Data.s "text2:"
  Data.s "*** I like PureBasic ***"
  Data.s #ETX$
  
EndDataSection

Re: Tool Create DataSections with AES Strings

Posted: Sun Dec 31, 2023 5:26 pm
by Caronte3D
Thanks! works nice :wink:

Re: Tool Create DataSections with AES Strings

Posted: Mon Jan 01, 2024 2:43 pm
by mk-soft
Update v1.03
- Build key and vector from data section

Happy new year :wink: