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 

RC4 Encryption
Goto page Previous  1, 2, 3  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Rajat



Joined: 28 Mar 2004
Posts: 1687

PostPosted: Fri Jun 17, 2005 3:53 am    Post subject: Reply with quote

if the earlier is working fine then its not necessary to update. if u still want to, then make sure u follow the new procedure well. (var name is 'data', result is in 'result' & u gosub rc4b)
_________________
Back to top
View user's profile Send private message
d3m0n1x



Joined: 04 May 2005
Posts: 16

PostPosted: Fri Jun 17, 2005 4:06 pm    Post subject: Reply with quote

I get it working after I run it the 1st time & save the password.

I load 1st gui encrypt password & save it 2 an *.ini file then when I load 2nd gui it decrypts the password & shows it decrypted but if I cancel then load 2nd Gui again it encrypts the password again or if I change the password, everytime I load 2nd Gui it continues encrypting it each time I load the gui. Embarassed

Can't figure out what I'm doing wrong.I guess I'll just have 2 stick with the earlier version till I finally figure out where I messed up. Rolling Eyes
Back to top
View user's profile Send private message
MIchael



Joined: 02 Mar 2005
Posts: 59

PostPosted: Fri Jun 17, 2005 5:47 pm    Post subject: Reply with quote

HEllo
How can we know whats wrong without knowing your code? Wink
MIchael
Back to top
View user's profile Send private message
Rajat



Joined: 28 Mar 2004
Posts: 1687

PostPosted: Sun Jun 19, 2005 8:13 pm    Post subject: Reply with quote

there's a small update to script. just the usage has been simplified.
_________________
Back to top
View user's profile Send private message
JGpo
Guest





PostPosted: Thu Jul 07, 2005 1:39 am    Post subject: Reply with quote

I just wanted to say that this code really helped me in a project I was working on. Thanks alot.
Back to top
Guest






PostPosted: Thu Jul 07, 2005 10:52 am    Post subject: Reply with quote

The script as a function, for easy #inclusion:

Code:
RC4Pass = wow_he-really+is cool_!
RC4Data = Rajat is cool

RC4(RC4Data,RC4Pass)
MsgBox, Changed`n`n%RC4Data%`n`nwith pass:`n`n%RC4Pass%`n`nto`n`n%RC4Result%

RC4Data = %RC4Result%

RC4(RC4Data,RC4Pass)
MsgBox, Changed`n`n%RC4Data%`n`nwith pass:`n`n%RC4Pass%`n`nto`n`n%RC4Result%

ExitApp

;___RC4_____________________________________

RC4(RC4Data,RC4Pass)
{
   global RC4Result
   ATrim = %A_AutoTrim%
   AutoTrim, Off
   BLines = %A_BatchLines%
   SetBatchlines, -1
   StringLen, RC4PassLen, RC4Pass
   Loop, 256
   {
      a := A_Index - 1
      Transform, ModVal, Mod, %a%, %RC4PassLen%
      ModVal ++
      StringMid, C, RC4Pass, %ModVal%, 1
      Transform, AscVar, Asc, %C%
      Key%a% = %AscVar%
      sBox%a% = %a%
   }
   b = 0
   Loop, 256
   {
      a := A_Index - 1
      TempVar := b + sBox%a% + Key%a%
      Transform, b, Mod, %TempVar%, 256
      T := sBox%a%
      sBox%a% := sBox%b%
      sBox%b% = %T%
   }
   StringLen, DataLen, RC4Data
   RC4Result =
   i = 0
   j = 0
   Loop, %DataLen%
   {
      TmpVar := i + 1
      Transform, i, Mod, %TmpVar%, 256
      TmpVar := sBox%i% + j
      Transform, j, Mod, %TmpVar%, 256
      TmpVar := sBox%i% + sBox%j%
      Transform, TmpVar2, Mod, %TmpVar%, 256
      k := sBox%TmpVar2%
      StringMid, TmpVar, RC4Data, %A_Index%, 1
      Transform, AscVar, Asc, %TmpVar%
      Transform, C, BitXOR, %AscVar%, %k%
      IfEqual, C, 0
         C = %k%
      Transform, ChrVar, Chr, %C%
      RC4Result = %RC4Result%%ChrVar%
   }
AutoTrim, %ATrim%
SetBatchlines, %BLines%
Return RC4Result
}
;___RC4_____________________________________
Back to top
Rajat



Joined: 28 Mar 2004
Posts: 1687

PostPosted: Thu Jul 07, 2005 7:38 pm    Post subject: Reply with quote

thanx for the function. the reason i didn't do so myself is that the first 2 loops are reqd only once for one pwd. so if you don't change the pwd but call the function several times for different data items then it does lots of unnecessary calculations, thus gets slower.
_________________
Back to top
View user's profile Send private message
twhyman



Joined: 07 Dec 2005
Posts: 339

PostPosted: Thu Dec 08, 2005 4:15 pm    Post subject: Reply with quote

Hi,

i did this script just to see how i can write encrypted passord to ini file and
restore it but somehow i can seem to make it....
can anyone help?

thanks
Twhyman

Code:

; Generated using SmartGUI Creator 3.5.1
IniRead, SUN, E:\Documents and Settings\admin\Desktop\test.ini, username, user ;Read User Name From ini
IniRead, SUP, E:\Documents and Settings\admin\Desktop\test.ini, pass, password ;Read Password From ini
Gui, Show, x182 y47 h149 w344, Password Encryption Test

Gui, Add, Text, x46 y37 w110 h20,User
Gui, Add, Edit, x176 y37 w130 h20 vGsUser, %SUN%
Gui, Add, Text, x46 y67 w110 h20,Password
Gui, Add, Edit, x176 y67 w130 h20 vGsPass, %SUP%
Gui, Add, Button, x126 y107 w110 h20 gSubmit,Submit
Return

Submit:
Gui, Submit, NoHide
GoSub RC4

PW = GsPass
Data = Rajat is cool

Gosub, RC4
MsgBox, %Result%

Data = %Result%

Gosub, RC4
MsgBox, %Result%


GuiClose:
IniWrite, %GsUser%, E:\Documents and Settings\admin\Desktop\test.ini, username, user ;Writes UserName to ini
IniWrite, %Result%, E:\Documents and Settings\admin\Desktop\test.ini, pass, password ;Writes Password to ini
ExitApp


;___RC4_____________________________________

RC4:
   ATrim = %A_AutoTrim%
   AutoTrim, Off
   BLines = %A_BatchLines%
   SetBatchlines, -1

   StringLen, PWLen, PW
   
   IfNotEqual, PW, %OldPW%
   {
      Loop, 256
      {
         a := A_Index - 1
         
         Transform, ModVal, Mod, %a%, %PWLen%
         ModVal ++
         
         StringMid, C, PW, %ModVal%, 1
         Transform, AscVar, Asc, %C%
   
         Key%a% = %AscVar%
         sBox%a% = %a%
      }
      
      b = 0
      Loop, 256
      {
         a := A_Index - 1
   
         TempVar := b + sBox%a% + Key%a%
         Transform, b, Mod, %TempVar%, 256
         
         T := sBox%a%
         sBox%a% := sBox%b%
         sBox%b% = %T%
      }
   
      OldPW = %PW%
   }

   StringLen, DataLen, Data
   
   Result =
   i = 0
   j = 0

   Loop, %DataLen%
   {
      TmpVar := i + 1
      Transform, i, Mod, %TmpVar%, 256

      TmpVar := sBox%i% + j
      Transform, j, Mod, %TmpVar%, 256

      TmpVar := sBox%i% + sBox%j%
      Transform, TmpVar2, Mod, %TmpVar%, 256
      
      k := sBox%TmpVar2%
      
      StringMid, TmpVar, Data, %A_Index%, 1
      Transform, AscVar, Asc, %TmpVar%

      Transform, C, BitXOR, %AscVar%, %k%

      IfEqual, C, 0
         C = %k%
      
      Transform, ChrVar, Chr, %C%

      Result = %Result%%ChrVar%
   }


   AutoTrim, %ATrim%
   SetBatchlines, %BLines%
Return

;___RC4_____________________________________
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Thu Dec 08, 2005 8:22 pm    Post subject: Reply with quote

There are problems with RC4 in binary mode: the encrypted text can contain non-printable characters. If you write them in a file and read back, different ones could map to the same character. If there is a NUL, it is treated as a string terminator, etc. If you insist on using RC4, you have to modify the code, such that the result is a stream of hex digits. But RC4 is not very secure, so why bother? Use instead TEA, the Tiny Encryption Algorithm. For texts use this, or that.
Back to top
View user's profile Send private message
Hansol



Joined: 27 Dec 2005
Posts: 36

PostPosted: Wed Apr 12, 2006 4:25 pm    Post subject: Reply with quote

Im trying to crypt a file, 24.txt, and then save it as 24Crypt.txt. Then i want to decrypt it and save it as 24Decrypt.txt. I cant understand why the If(Data = Result) doesnt seems to be true.

Code:
PW = password

FileRead, Data, %A_WorkingDir%\24.txt

Gosub, RC4

Loop, Parse, Result, `n, `r

  FileAppend, %A_LoopField%`n, %A_WorkingDir%\24Crypt.txt
}
;-----------------------

FileRead, Data, %A_WorkingDir%\24Crypt.txt

StringTrimRight, Data, Data, 2  ;This line is supposed to take away the last line feed.

If (Data = Result) Then   
  MsgBox, They are exactly alike
MsgBox, %Data%
MsgBox, %Result%

;Data = %Result%    ;If I uncomment this it will work.
Gosub, RC4

Loop, Parse, Result, `n, `r

  FileAppend, %A_LoopField%`n, %A_WorkingDir%\24Decrypt.txt
}

ExitApp


;___RC4_____________________________________

RC4:
   ATrim = %A_AutoTrim%
   AutoTrim, Off
   BLines = %A_BatchLines%
   SetBatchlines, -1

   StringLen, PWLen, PW
   
   IfNotEqual, PW, %OldPW%
   {
      Loop, 256
      {
         a := A_Index - 1
         
         Transform, ModVal, Mod, %a%, %PWLen%
         ModVal ++
         
         StringMid, C, PW, %ModVal%, 1
         Transform, AscVar, Asc, %C%
   
         Key%a% = %AscVar%
         sBox%a% = %a%
      }
      
      b = 0
      Loop, 256
      {
         a := A_Index - 1
   
         TempVar := b + sBox%a% + Key%a%
         Transform, b, Mod, %TempVar%, 256
         
         T := sBox%a%
         sBox%a% := sBox%b%
         sBox%b% = %T%
      }
   
      OldPW = %PW%
   }

   StringLen, DataLen, Data
   
   Result =
   i = 0
   j = 0

   Loop, %DataLen%
   {
      TmpVar := i + 1
      Transform, i, Mod, %TmpVar%, 256

      TmpVar := sBox%i% + j
      Transform, j, Mod, %TmpVar%, 256

      TmpVar := sBox%i% + sBox%j%
      Transform, TmpVar2, Mod, %TmpVar%, 256
      
      k := sBox%TmpVar2%
      
      StringMid, TmpVar, Data, %A_Index%, 1
      Transform, AscVar, Asc, %TmpVar%

      Transform, C, BitXOR, %AscVar%, %k%

      IfEqual, C, 0
         C = %k%
      
      Transform, ChrVar, Chr, %C%

      Result = %Result%%ChrVar%
   }


   AutoTrim, %ATrim%
   SetBatchlines, %BLines%

Return

;___RC4_____________________________________


24.txt (Dont mind the text, just copied the post above)
Code:
There are problems with RC4 in binary mode: the encrypted text
can contain non-printable characters.
If you write them in a file and read back,
different ones could map to the same character.
If there is a NUL, it is treated as a string terminator, etc.
If you insist on using RC4, you have to modify the code,
such that the result is a stream of hex digits.
But RC4 is not very secure, so why bother?
Use instead TEA, the Tiny Encryption Algorithm.
For texts use this, or that.
Back to top
View user's profile Send private message MSN Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Wed Apr 12, 2006 4:38 pm    Post subject: Reply with quote

As I said, this code fails if there is a NUL character in the ciphertext. Use instead the hex-stream version.
Back to top
View user's profile Send private message
Hansol



Joined: 27 Dec 2005
Posts: 36

PostPosted: Wed Apr 12, 2006 5:49 pm    Post subject: Reply with quote

Thank You! Smile
Back to top
View user's profile Send private message MSN Messenger
Dippy46



Joined: 06 Jul 2004
Posts: 171
Location: Manchester, England.

PostPosted: Wed Apr 12, 2006 6:17 pm    Post subject: Reply with quote

Hi All

If anyone wants to do some serious encryption on very large binary et al, then maybe this is for you 'AESY.EXE'.


http://www.wzw.tum.de/~syring/win32/

Thanx
Dave.
_________________
Simple ideas lie within reach, only of complex minds
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Wed Apr 12, 2006 6:41 pm    Post subject: Reply with quote

Thanks, Dave! It works, fast, it is small, but I wish I could specify the output file. It just overwrites the original, (which is what you want most of the time). You cannot test the program on itself ("Couldn't open file with CreateFile()"). Error handling is not reliable: sometimes, if you attempt to encrypt a non-existent file, you would not know that it failed. ErrorLevel is still 0. I could not find out, which AES version it uses, how the key is converted to binary key, etc. Otherwise, it is a great little program.
Back to top
View user's profile Send private message
Dippy46



Joined: 06 Jul 2004
Posts: 171
Location: Manchester, England.

PostPosted: Thu Apr 20, 2006 8:12 pm    Post subject: Reply with quote

Hi All,

@Laszlo

Bit late getting back to you on this sorry!.

1./ If you make a copy of aesy.exe somewhere else then you can encrypt it. Basically it checks to make sure you don't cut the branch your sitting on.
2./ The code below works for me in every situation so far encountered.

Code:

;; Win xp pro sp0
;; Ahk Version 1.0.43.08


RunWait, %comspec% /c aesy isExist.dat  , , min
   MsgBox %errorlevel%

;; returns 0

RunWait, %comspec% /c aesy noExist.dat  , , min
   MsgBox %errorlevel%

;; returns 1



3./ Now the really good stuff Cool

http://fp.gladman.plus.com/AES/index.htm

Enjoy !

Dave.
_________________
Simple ideas lie within reach, only of complex minds
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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