AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

encrypting strings in script file?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
boardtc



Joined: 31 Jan 2006
Posts: 12

PostPosted: Fri May 25, 2007 10:19 am    Post subject: encrypting strings in script file? Reply with quote

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.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5892

PostPosted: Fri May 25, 2007 10:30 am    Post subject: Reply with quote

See: RC4 encryption to hex stream by Laszlo

Smile

PS: It is better to compile such scripts.
Back to top
View user's profile Send private message
boardtc



Joined: 31 Jan 2006
Posts: 12

PostPosted: Fri May 25, 2007 10:46 am    Post subject: Reply with quote

Skan wrote:
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.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5892

PostPosted: Fri May 25, 2007 11:01 am    Post subject: Reply with quote

boardtc wrote:
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:

Code:
Clipboard := RC4txt2hex( "ThisIsMyLongPassword123", "boardtc")

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.

Code:
MyPassWord := RC4hex2txt("d90ba759853b387cbd2bd1ec42f10176d5c92e29128840", "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.

Smile
Back to top
View user's profile Send private message
boardtc



Joined: 31 Jan 2006
Posts: 12

PostPosted: Fri May 25, 2007 11:10 am    Post subject: Reply with quote

That's fantastic, thanks for taking the time, dead clear! let's make this one a sticky Very Happy
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Fri May 25, 2007 11:58 am    Post subject: Reply with quote

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:
Code:
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!).
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group