Obfuscating Code

Not to spoil Arbed, which is a really nice tool that every Xojo developer should have, here it’s an IDE script which makes a very basic string obfuscation.

Just select the string to obfuscate in code editor and run the script.

  ////////////////////////////////////////////////////////////////////////////////
  
  // simple string obfuscation
  
  ////////////////////////////////////////////////////////////////////////////////
  
  dim selectedText as string
  dim obfuscatedText as string
  dim obfuscChar as string
  dim obfuscLineLen as integer
  dim selectionStart as integer
  dim currentCode as string
  dim ascValue as integer
  dim i, n as integer
  
  const MAX_LINE_LEN = 80
  
  if selLength > 0 then
    
    selectedText = selText
    selectionStart = selStart
    
    if selLength > 1 and left(selectedText, 1) = """" and right(selectedText, 1) = """" then
      selectedText = mid(selectedText, 2, len(selectedText) - 2)
    end if
    
    n = len(selectedText)
    for i = 1 to n
      
      if obfuscLineLen > MAX_LINE_LEN then
        obfuscLineLen = 0
        obfuscatedText = obfuscatedText + " _" + endOfLine
      end if
      
      if obfuscatedText <> "" then
        obfuscatedText = obfuscatedText + "+"
        obfuscLineLen = obfuscLineLen + 1
      end if
      
      ascValue = asc(mid(selectedText, i, 1))
      
      if ascValue < 128 then
        obfuscChar = "chr(" + str(ascValue) + ")"
      else
        obfuscChar = "encodings.UTF8.chr(" + str(ascValue) + ")"
      end if
      obfuscLineLen = obfuscLineLen + len(obfuscChar)
      
      obfuscatedText = obfuscatedText + obfuscChar
      
    next
    
    selText = obfuscatedText
    
    currentCode = Text
    
    i = selectionStart
    while i > -1
      if mid(currentCode, i, 1) = endOfLine then
        if mid(currentCode, i-1, 1) <> "_" then
          exit
        end if
      end if
      i = i -1
    wend
    
    currentCode = left(currentCode, i) _
    + "// obfuscated string = " + selectedText + endOfLine _
    + mid(currentCode, i+1)
    
    Text = currentCode
    
  end if