AutoHotkey Community

It is currently May 27th, 2012, 3:23 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: May 31st, 2010, 8:02 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
I have to encode & decode UNICODE string.
I tried Laszlo's RC4 encryption and [VxE]'s Encryption Function in AHK_L unicode but they don't work correctly for some characters.

Below is example code that uses Laszlo's RC4. Characters žccšd are not encoded & decoded correctly.
Can somebody help? Is there any AHK encryption algorithm that supports unicode? Thanks in advance.
Code:
RC4Pass = MyPassword
RC4Data = Some croatian/serbian characters: žćčšđ.

RC4Enc := RC4txt2hex(RC4Data,RC4Pass)
RC4Dec := RC4hex2txt(RC4Enc,RC4Pass)
MsgBox %RC4Data%`n`nEncypted to`n`n%RC4Enc%`n`nDecypted to`n`n%RC4Dec%
ExitApp

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
     ,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
   Loop Parse, Data
      i := A_Index & 255
     ,j := sBox%i% + j  & 255
     ,k := sBox%i% + sBox%j%  & 255
     ,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
     ,Result .= SubStr(Asc(A_LoopField)^sBox%k%, -1, 2)
   StringReplace Result, Result, x, 0, All
   SetFormat Integer, %Format%
   Return Result
}

RC4hex2txt(Data,Pass) {
   b := 0, j := 0, x := "0x"
   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
     ,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
   Loop % StrLen(Data)//2
      i := A_Index  & 255
     ,j := sBox%i% + j  & 255
     ,k := sBox%i% + sBox%j%  & 255
     ,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
     ,Result .= Chr((x . SubStr(Data,2*A_Index-1,2)) ^ sBox%k%)
   Return Result
}


Last edited by Learning one on June 1st, 2010, 2:04 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2010, 9:05 pm 
Offline

Joined: June 12th, 2008, 1:48 am
Posts: 27
Since I did run into a similar problem, one solution would be to encode the string in UTF-7 first, then those functions should work again.

Code:
MsgBox, % p:=utf7encode("žccšd")
MsgBox, % utf7decode(p)
Return

Utf7Encode(s){
  utf7Size := DllCall("WideCharToMultiByte", "UInt", 65000, "UInt", 0, "UInt", &s, "Int", -1, "Int", 0, "Int", 0, "Int", 0, "Int", 0)
  VarSetCapacity(utf7String, utf7Size)
  DllCall("WideCharToMultiByte", "UInt", 65000, "UInt", 0, "UInt", &s, "Int", -1, "Str", utf7String, "Int", utf7Size, "Int", 0, "Int", 0)
  UniSize := DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &utf7String, "Int", -1, "Int", 0, "Int", 0)
  VarSetCapacity(UniBuf, UniSize * 2)
  DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &utf7String, "Int", -1, "UInt", &UniBuf, "Int", UniSize)
  return UniBuf
}

Utf7Decode(s){
  AnsiSize := DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", &s, "Int", -1, "Int", 0, "Int", 0, "Int", 0, "Int", 0)
  VarSetCapacity(AnsiString, AnsiSize)
  DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", &s, "Int", -1, "Str", AnsiString, "Int", AnsiSize, "Int", 0, "Int", 0)
  UniSize := DllCall("MultiByteToWideChar", "UInt", 65000, "UInt", 0, "UInt", &AnsiString, "Int", -1, "Int", 0, "Int", 0)
  VarSetCapacity(UniBuf, UniSize * 2)
  DllCall("MultiByteToWideChar", "UInt", 65000, "UInt", 0, "UInt", &AnsiString, "Int", -1, "UInt", &UniBuf, "Int", UniSize)
  return UniBuf
}


This works only with AHK_L !!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2010, 2:03 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
@dadepp: Thank you! :D

So here it is;
UNICODE encryption for AutoHotkey_L unicode
Code:
;===Description=========================================================================
; UNICODE encryption for AutoHotkey_L unicode
; put together by Learning one using Laszlo's and dadepp's code. Thank you!


; ===Example===
Pass = MyPassword
Data = AutoHotkey_L unicode is great! 123 žćčšđ ŽĆČŠĐ

EncodedData := UnicodeEncode(Data,Pass)
MsgBox, % "Encoded to:`n" EncodedData

DecodedData := UnicodeDecode(EncodedData,Pass)
MsgBox, % "Decoded to:`n" DecodedData
ExitApp





;===Functions===========================================================================
UnicodeEncode(Data,Pass) {
   utf7Data:=utf7encode(Data)
   return RC4txt2hex(utf7Data,Pass)
}

UnicodeDecode(Data,Pass) {
   Decoded2utf7 := RC4hex2txt(Data,Pass)
   return utf7decode(Decoded2utf7)
}

Utf7Encode(s){
  utf7Size := DllCall("WideCharToMultiByte", "UInt", 65000, "UInt", 0, "UInt", &s, "Int", -1, "Int", 0, "Int", 0, "Int", 0, "Int", 0)
  VarSetCapacity(utf7String, utf7Size)
  DllCall("WideCharToMultiByte", "UInt", 65000, "UInt", 0, "UInt", &s, "Int", -1, "Str", utf7String, "Int", utf7Size, "Int", 0, "Int", 0)
  UniSize := DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &utf7String, "Int", -1, "Int", 0, "Int", 0)
  VarSetCapacity(UniBuf, UniSize * 2)
  DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &utf7String, "Int", -1, "UInt", &UniBuf, "Int", UniSize)
  return UniBuf
}

Utf7Decode(s){
  AnsiSize := DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", &s, "Int", -1, "Int", 0, "Int", 0, "Int", 0, "Int", 0)
  VarSetCapacity(AnsiString, AnsiSize)
  DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", &s, "Int", -1, "Str", AnsiString, "Int", AnsiSize, "Int", 0, "Int", 0)
  UniSize := DllCall("MultiByteToWideChar", "UInt", 65000, "UInt", 0, "UInt", &AnsiString, "Int", -1, "Int", 0, "Int", 0)
  VarSetCapacity(UniBuf, UniSize * 2)
  DllCall("MultiByteToWideChar", "UInt", 65000, "UInt", 0, "UInt", &AnsiString, "Int", -1, "UInt", &UniBuf, "Int", UniSize)
  return UniBuf
}

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
     ,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
   Loop Parse, Data
      i := A_Index & 255
     ,j := sBox%i% + j  & 255
     ,k := sBox%i% + sBox%j%  & 255
     ,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
     ,Result .= SubStr(Asc(A_LoopField)^sBox%k%, -1, 2)
   StringReplace Result, Result, x, 0, All
   SetFormat Integer, %Format%
   Return Result
}

RC4hex2txt(Data,Pass) {
   b := 0, j := 0, x := "0x"
   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
     ,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
   Loop % StrLen(Data)//2
      i := A_Index  & 255
     ,j := sBox%i% + j  & 255
     ,k := sBox%i% + sBox%j%  & 255
     ,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
     ,Result .= Chr((x . SubStr(Data,2*A_Index-1,2)) ^ sBox%k%)
   Return Result
}

Should I post that in Scripts & Functions?


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: rbrtryn and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group