Tool Create DataSections with AES Strings

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Tool Create DataSections with AES Strings

Post 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
Last edited by mk-soft on Mon Jan 01, 2024 2:42 pm, edited 3 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Tool Create DataSections with AES Strings

Post by Caronte3D »

Thanks! works nice :wink:
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Tool Create DataSections with AES Strings

Post by mk-soft »

Update v1.03
- Build key and vector from data section

Happy new year :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply