Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

encrypting strings in script file?


  • Please log in to reply
5 replies to this topic
boardtc
  • Members
  • 35 posts
  • Last active: Oct 08 2019 10:35 AM
  • Joined: 31 Jan 2006
I am storing bank numbers and the like for cutting and pasting with shortcuts. is there a way to have these encrypted in my script file, so if the script is opened, the values are not discernible?

Thanks, Tom.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
See: RC4 encryption to hex stream by Laszlo

:)

PS: It is better to compile such scripts.

boardtc
  • Members
  • 35 posts
  • Last active: Oct 08 2019 10:35 AM
  • Joined: 31 Jan 2006

See: RC4 encryption to hex stream by Laszlo


Thank you. So I include RC4txt2hex(Data,Pass) in my script...but how do I use this? Where is the password stored, etc?

Please would it be possible to outline the steps to use this without having to spend a few hours trying to understand what's going on. Appreciated.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Please would it be possible to outline the steps to use this without having to spend a few hours trying to understand what's going on.


Sure.. Just two steps

Step1: Create the following temporary script and run it:

[color=red]Clipboard := RC4txt2hex( "ThisIsMyLongPassword123", "boardtc")[/color]

RC4txt2hex(Data,Pass) { 
   Format := A_FormatInteger 
   SetFormat Integer, Hex 
   b := 0, j := 0 
   VarSetCapacity(Result,StrLen(Data)*2) 
   Loop 256 { 
      a := A_Index - 1 
      Key%a% := Asc(SubStr(Pass, Mod(a,StrLen(Pass))+1, 1)) 
      sBox%a% := a 
   } 
   Loop 256 { 
      a := A_Index - 1 
      b := b + sBox%a% + Key%a%  & 255 
      T := sBox%a% 
      sBox%a% := sBox%b% 
      sBox%b% := T 
   } 
   Loop Parse, Data 
   { 
      i := A_Index & 255 
      j := sBox%i% + j  & 255 
      k := sBox%i% + sBox%j%  & 255 
      Result .= SubStr(Asc(A_LoopField)^sBox%k%, -1, 2) 
   } 
   StringReplace Result, Result, x, 0, All 
   SetFormat Integer, %Format% 
   Return Result 
}

The above script encrypts the string ThisIsMyLongPassword123 and stores it in the clipboard

Step2: The following code would reside in your actual script.

MyPassWord := RC4hex2txt("[color=red]d90ba759853b387cbd2bd1ec42f10176d5c92e29128840[/color]", "boardtc" )
MsgBox, % MyPassWord

RC4hex2txt(Data,Pass) 
{ 
   ATrim = %A_AutoTrim% 
   AutoTrim Off 
   Loop 256 
   { 
      a := A_Index - 1 
      StringMid C, Pass, Mod(a,StrLen(Pass))+1, 1 
      Key%a% := Asc(C) 
      sBox%a% = %a% 
   } 
   b = 0 
   Loop 256 
   { 
      a := A_Index - 1 
      b := b + sBox%a% + Key%a%  & 255 
      T := sBox%a% 
      sBox%a% := sBox%b% 
      sBox%b% = %T% 
   } 
   i = 0 
   j = 0 
   Loop Parse, Data 
   { 
      If (A_Index & 1) 
         C = 0x%A_LoopField% 
      Else { 
         i := i + 1  & 255 
         j := sBox%i% + j  & 255 
         k := sBox%i% + sBox%j%  & 255 
         C := (C A_LoopField) ^ sBox%k% 
         IfEqual C,0, SetEnv C, % sBox%k% 
         Result := Result Chr(C) 
      } 
   } 
   AutoTrim %ATrim% 
   Return Result 
}

The red colored hex string ( d90ba759853b387cbd2bd1ec42f10176d5c92e29128840 ) was actually pasted from the clipboard ( with CTRL+V )

Hope you get it.

:)

boardtc
  • Members
  • 35 posts
  • Last active: Oct 08 2019 10:35 AM
  • Joined: 31 Jan 2006
That's fantastic, thanks for taking the time, dead clear! let's make this one a sticky :D

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Of course, this protection is very brittle (just add a MsgBox to see the clear password...), but perfect against casual looks and people not knowing AHK.
Personally, I use two functions:
Crypt(_string)
{
	local f, r

	f := A_FormatInteger
	SetFormat Integer, H
	Loop Parse, _string
	{
		r .= Asc(A_LoopField) ^ Asc(SubStr(A_ScriptName, A_Index, 1)) + 256
	}
	StringReplace r, r, 0x1, , All
	StringUpper r, r
	SetFormat Integer, %f%
	Return r
}
; copy clear password, hit Ctrl+Alt+k to get in clipboard the encrypted string to paste in the script
^!k:: Clipboard := Crypt(Clipboard)

Decrypt(_string)
{
	local d, r

	d := RegExReplace(_string, "..", "0x$0!")
	StringTrimRight d, d, 1
	Loop Parse, d, !
	{
		r .= Chr((A_LoopField + 0) ^ Asc(SubStr(A_ScriptName, A_Index, 1)))
	}
	Return r
}

; Usage: (the data here is random!)
Some & Hotkey:: SendRaw % Decrypt("4457544854")
That's more shadowing than encryption, but as I wrote, it is good enough for internal use (not for sending the data over Internet!).
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")