Obfuscation

You gave me some good ideas that I modified slightly. I didn’t see the benefit of the multiplier vs. the adder (perhaps you can convince me), but the comments were a good idea. More importantly, I modified to paste the code directly above the highlighted string and put most of it in an If statement. The upshot is that you can encode several strings in the same method without an error.

  Function RndInRange (startIndex As Integer, endIndex As Integer) As Integer
    dim d as Double = Rnd
    dim range as Integer = endIndex - startIndex
    return Round( range * d ) + startIndex
  End Function
  
  dim origString as String = SelText
  if origString.Trim = "" then
    print "Select some text first."
    return
  end if
  
  origString = origString.ReplaceAll( """""", """" )
  dim chars() as String = Split( origString, "" )
  
  dim startQuote as boolean = chars( 0 ) = """"
  dim endQuote as boolean = chars( chars.Ubound ) = """"
  
  if endQuote then
    chars.Remove chars.Ubound
  end if
  
  if chars.Ubound <> -1 and startQuote then
    chars.Remove 0
  end if
  
  if chars.Ubound = -1 then
    print "Select some valid text first."
    return
  end if
  
  dim stringToEncode as String = Join( chars, "" )
  dim b as String = ShowDialog( "You are about to encode this string. Proceed?", stringToEncode, "Yes", "No", "" )
  if b = "No" then
    return
  end if
  
  dim index as Integer
  dim codeArr() as String
  dim indexArr() as String
  dim addArr() as String
  dim randomizerArr() as Integer
  for index = 0 to chars.Ubound
    dim thisAdd as Integer = RndInRange( 64001, 100000 )
    codeArr.Append Str( Asc( chars( index ) ) + thisAdd )
    indexArr.Append Str( index )
    addArr.Append Str( thisAdd )
    randomizerArr.Append RndInRange( 0, chars.Ubound * 100 )
  next index
  
  randomizerArr.SortWith( codeArr, indexArr, addArr )
  
  // Construct the code
  dim eol as String = EndOfLine
  dim resultArr() as String
  
  resultArr.Append "dim decodedString as String"
  resultArr.Append eol
  resultArr.Append eol
  
  resultArr.Append "// Encoding for value: "
  resultArr.Append stringToEncode
  resultArr.Append eol
  
  resultArr.Append "if True then"
  resultArr.Append eol
  
  resultArr.Append "dim codeArr() as Integer = Array( "
  resultArr.Append Join( codeArr, ", " )
  resultArr.Append " )"
  resultArr.Append eol
  
  resultArr.Append "dim adderArr() as Integer = Array( "
  resultArr.Append Join( addArr, ", " )
  resultArr.Append " )"
  resultArr.Append eol
  
  resultArr.Append "dim indexArr() as Integer = Array( "
  resultArr.Append Join( indexArr, ", " )
  resultArr.Append " )"
  resultArr.Append eol
  
  resultArr.Append "indexArr.SortWith codeArr, adderArr"
  resultArr.Append eol
  
  resultArr.Append eol
  
  resultArr.Append "dim decodedChars() as String"
  resultArr.Append eol
  
  resultArr.Append "for i as Integer = 0 to codeArr.Ubound"
  resultArr.Append eol
  
  resultArr.Append "decodedChars.Append Chr( codeArr( i ) - adderArr( i ) )"
  resultArr.Append eol
  
  resultArr.Append "next i"
  resultArr.Append eol
  
  resultArr.Append eol
  
  resultArr.Append "decodedString = Join( decodedChars, """" )"
  resultArr.Append eol
  
  resultArr.Append "end if // True"
  resultArr.Append eol
  
  resultArr.Append "// End Encoding for value: "
  resultArr.Append stringToEncode
  resultArr.Append eol
  
  dim result as String = Join( resultArr, "" )
  
  b = ShowDialog( "Paste into this method or Copy to clipboard?", result, "Paste", "Cancel", "Copy" )
  select case b
  case "Cancel"
    return
  case "Copy"
    Clipboard = result
  case "Paste"
    // See if we need the initial declaration
    if Text.InStr( resultArr( 0 ) ) <> 0 then
      for index = 1 to 2
        resultArr.Remove 0
      next index
      result = Join( resultArr, "" )
    end if
    
    // Figure out where we should paste
    dim curText as String = Text
    dim curSelStart as Integer = SelStart
    dim newSelStart as Integer
    for index = curSelStart downto 1
      dim curChar as String = curText.Mid( index, 1 )
      if curChar = Chr( 13 ) or curChar = Chr( 10 ) then
        newSelStart = index
        exit
      end if
    next index
    
    SelText = "decodedString"
    SelStart = newSelStart
    SelLength = 0
    SelText = result
    SelText = eol
  end select
1 Like