Sign in to follow this  
Followers 0

Rc4 Text Encryption

13 posts in this topic

Posted (edited)

I have made an RC4 Text Encryption UDF. I know RC4 isn't the strongest encryption but anyway here it is:

;===============================================================================
;
; Function Name:   _StringEncryptRC4
; Description::    Encrypts text using RC4 Encryption
; Parameter(s):    $text, $encryptkey
; Requirement(s):  AutoIt
; Return Value(s): Encrypted String
; Author(s):       RazerM
;
;===============================================================================
;
Func _StringEncryptRC4($text, $encryptkey)
    Local $sbox[256]
    Local $key[256]
    Local $temp
    Local $a
    Local $i
    Local $j
    Local $k
    Local $cipherby
    Local $cipher
    
    $i = 0
    $j = 0
    
    __RC4Initialize($encryptkey, $key, $sbox)
    
    For $a = 1 To StringLen($text)
        $i = Mod(($i + 1),256)
        $j = Mod(($j + $sbox[$i]),256)
        $temp = $sbox[$i]
        $sbox[$i] = $sbox[$j]
        $sbox[$j] = $temp
        
        $k = $sbox[Mod(($sbox[$i] + $sbox[$j]),256)]
        
        $cipherby = BitXOR(Asc(StringMid($text, $a, 1)),$k)
        $cipher = $cipher & Chr($cipherby)
    Next
    
    Return _StringToHexEx($cipher)
EndFunc   ;==>_StringEncryptRC4

;===============================================================================
;
; Function Name:   _StringDecryptRC4
; Description::    Decrypts text using RC4 Encryption
; Parameter(s):    $text, $encryptkey
; Requirement(s):  AutoIt
; Return Value(s): Decrypted String
; Author(s):       RazerM
; Note(s):         RC4 uses the same algorithm to encrypt and decrypt
;
;===============================================================================
;

Func _StringDecryptRC4($text, $encryptkey)
    Local $sbox[256]
    Local $key[256]
    Local $temp
    Local $a
    Local $i
    Local $j
    Local $k
    Local $cipherby
    Local $cipher
    $text = _HexToStringEx($text)
    
    $i = 0
    $j = 0
    
    __RC4Initialize($encryptkey, $key, $sbox)
    
    For $a = 1 To StringLen($text)
        $i = Mod(($i + 1),256)
        $j = Mod(($j + $sbox[$i]),256)
        $temp = $sbox[$i]
        $sbox[$i] = $sbox[$j]
        $sbox[$j] = $temp
        
        $k = $sbox[Mod(($sbox[$i] + $sbox[$j]),256)]
        
        $cipherby = BitXOR(Asc(StringMid($text, $a, 1)),$k)
        $cipher = $cipher & Chr($cipherby)
    Next
    Return $cipher
EndFunc   ;==>_StringDecryptRC4


; Helper function
Func __RC4Initialize($strPwd, ByRef $key, ByRef $sbox)
    Dim $tempSwap
    Dim $a
    Dim $b
    
    $intLength = StringLen($strPwd)
    For $a = 0 To 255
        $key[$a] = Asc(StringMid($strPwd, (Mod($a,$intLength))+1, 1))
        $sbox[$a] = $a
    Next
    
    $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
    Next
EndFunc   ;==>__RC4Initialize

Func _HexToStringEx($strHex)
    Return BinaryToString("0x" & $strHex)
EndFunc   ;==>_HexToStringEx

Func _StringToHexEx($strChar)
    Return Hex(StringToBinary($strChar))
EndFunc   ;==>_StringToHexEx oÝ÷ Øw«z+-ç±jjey«­¢+Ø¥¹±ÕÅÕ½ÐíIйÔÌÅÕ½Ðì(ÀÌØíÑáÐô%¹ÁÕÑ ½à ÅÕ½ÐíQáÐÅÕ½Ðì°ÅÕ½Ðí¹ÑÈÑáÐѼ¹ÉåÁÐÅÕ½Ðì¤(ÀÌØí¹ÉåÁÑ­äô%¹ÁÕÑ  ½à ÅÕ½Ðí-äÅÕ½Ðì°ÅÕ½Ðí¹ÑÈ­äѼ¹ÉåÁÐÑ¡ÑáÐÅÕ½Ðì¤(ÀÌØíÉÐôQ¥µÉ%¹¥Ð ¤(ÀÌØí¹ÉåÁÑô}MÑÉ¥¹¹ÉåÁÑIÐ ÀÌØíÑáаÀÌØí¹ÉåÁÑ­ä¤)5Í ½à À°ÅÕ½Ðí¹ÉåÁÑ¥½¸Q½½¬èÅÕ½Ðì°Q¥µÉ¥ ÀÌØíÉФµÀìÅÕ½ÐìµÌÅÕ½Ðì¤)5Í    ½à À°ÅÕ½Ðí¹ÉåÁÑÅÕ½Ðì°ÅÕ½Ðí¹ÉåÁÑÑáÐ¥ÌèÅÕ½ÐìµÀìÀÌØí¹ÉåÁѤ)5Í   ½à À°ÅÕ½ÐíÉåÁÑÅÕ½Ðì°ÅÕ½ÐíÉåÁÑÑáÐ¥ÌèÅÕ½ÐìµÀì}MÑÉ¥¹ÉåÁÑIÐ ÀÌØí¹ÉåÁÑ°ÀÌØí¹ÉåÁѭ䤤((
Edited by RazerM

Share this post


Link to post
Share on other sites



Posted

Very nice and fast RazorM!

Share this post


Link to post
Share on other sites

Posted

Thanks. It is about half the time of the built in _StringEncrypt function.

Share this post


Link to post
Share on other sites

Posted

really nice!

Share this post


Link to post
Share on other sites

Posted

Nicely done RazerM.

Share this post


Link to post
Share on other sites

Posted (edited)

Very nice indeed, nice job Razer.

Edit:

A suggestion though, since you're not using unique vars, is to keep them in a local scope.

Edited by SmOke_N

Share this post


Link to post
Share on other sites

Posted

I updated it. Thanks SmOke_N

Share this post


Link to post
Share on other sites

Posted

Faster string->hex functions:

Func _HexToStringEX($strHex)
   Return BinaryString("0x" & $strHex)
EndFunc

Func _StringToHexEx($strChar)
   Return Hex(BinaryString($strChar))
EndFunc
:whistle:

Share this post


Link to post
Share on other sites

Posted

I would never have thought of doing that! Im implementing it now

Share this post


Link to post
Share on other sites

Posted

Hey Razerm

It seems this script no longer works with the latest released version of the Autoit??

Return [b]BinaryString[/b]("0x" & $strHex)

The BinaryString function no longer works??

Any ideas?

Share this post


Link to post
Share on other sites

Posted (edited)

I'll update in a few minutes.

Edit: Updated

Edited by RazerM

Share this post


Link to post
Share on other sites

Posted

Gonna make a GUI for this as well.

Share this post


Link to post
Share on other sites

Posted

It doesn't need one, but if you do, don't post it in this topic.

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  
Followers 0