Jump to content
Sign in to follow this  

Working RC4 String Encryption

Recommended Posts

A while back I posted an RC4 function, but it wasn't 100%. This was due to AutoIt not supporting NULs. Some additional work resulted in this itteration. To get around the NUL issue the function returns a HEX string rather than a character string. By the way this makes it easy to read/write encrypted text to/from the registry as REG_BINARY.

Test Code:

$pw = "~#[.ab!@MNmn6UVuv2-<EFef`$KLkl7&\XwABW{,CDcdx1+>}/GHgh9%]IJij8^|YZyz0=?*:OPop 5(""QRqr4);STst3_'"
$text = "AutoIt has been designed to work on Windows 95, 98, ME, NT 4, 2000, XP and 2003."

$Encrypted = RC4($text, $pw, 0)
MsgBox(0,"Encrypted Text",$Encrypted)
$Decrypted = RC4($Encrypted, $pw, 1)
MsgBox(0,"Decrypted Text",$Decrypted)
MsgBox(0,"Original = Decrypted",$text = $Decrypted)

RC4 Function

Func RC4($Data, $Phrase, $Decrypt)
   Local $a, $b, $i, $j, $k, $cipherby, $cipher
   Local $tempSwap, $temp, $PLen
   Local $sbox[256], $key[256]
   $PLen = StringLen($Phrase)
   For $a = 0 To 255
      $key[$a] = Asc(StringMid($Phrase, Mod($a, $PLen) + 1, 1))
      $sbox[$a] = $a
   $b = 0
   For $a = 0 To 255
      $b = Mod( ($b + $sbox[$a] + $key[$a]), 256)
      $tempSwap = $sbox[$a]
      $sbox[$a] = $sbox[$b]
      $sbox[$b] = $tempSwap
   If $Decrypt Then
      For $a = 1 To StringLen($Data) Step 2
         $i = Mod(($i + 1), 256)
         $j = Mod(($j + $sbox[$i]), 256)
         $k = $sbox[Mod(($sbox[$i] + $sbox[$j]), 256)]
         $cipherby = BitXOR(Dec(StringMid($Data, $a, 2)), $k)
         $cipher = $cipher & Chr($cipherby)
      For $a = 1 To StringLen($Data)
         $i = Mod(($i + 1), 256)
         $j = Mod(($j + $sbox[$i]), 256)
         $k = $sbox[Mod(($sbox[$i] + $sbox[$j]), 256)]
         $cipherby = BitXOR(Asc(StringMid($Data, $a, 1)), $k)
         $cipher = $cipher & Hex($cipherby, 2)
   Return $cipher
EndFunc  ;==>RC4
Edited by bcording

Share this post

Link to post
Share on other sites

I don't get it, how do you use it?


His post shows how to use it. You have to use his function. Just like all UDF's (user defined functions). Look in the first code box. It shows you what to do. The second one just shows you how it works and gives you the code so you can implement it.


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...