Jump to content

Recommended Posts

Posted

I found an interesting RC4 implementation on Github that was available in PHP and Javascript in a compatible way. So I ported it to AutoIt keeping it compatible with the PHP/Javascript versions.

The problem with RC4 is that most of the implementations are only based on the original spec, and not compatible to it. Therefore if you are developing something that must communicate between different platforms (for example, encrypt at the AutoIt side and decrypt at the PHP side), you're usually out of luck.

But what about Crypt.au3?

It uses Windows implementation, which is also not standard and therefore isn't compatible with the PHP/JS versions as well (with "compatible" I mean: "giving the same string and encryption key, you'll get the same output").

What about huge data/files?

I'd suggest not to use this version for huge amounts of data since it's entirely AU3 based and may be slow for that.

So why would I use this version?

Because you are developing something that uses PHP, Javascript and/or AutoIt and they must speak the same language (I mean: they must produce the same outputs and be able to decrypt strings encrypted on a different programming language).

But it's giving me unreadable outputs.

Yeah RC4 does it. It's up to you to use Base64 or hex encoding if you really need it.

Then why don't I use base64 directly?

Base64 isn't meant to protect data. It's meant to make binary data easy and safe to store by converting it to readable characters. That's why it doesn't include password protection.

Here's the code:

Func rc4($sKey, $sStr)
    Local $s[256], $j = 0, $x, $res, $y, $i
    Local $uBound
    For $i = 0 To 255
        $s[$i] = $i
    Next
    For $i = 0 To 255
        $j = Mod(($j + $s[$i] + Asc(StringMid($sKey, Mod($i, StringLen($sKey))+1, 1))), 256)
        $x = $s[$i]
        $s[$i] = $s[$j]
        $s[$j] = $x
    Next
    $i = 0
    $j = 0
    For $y = 0 To StringLen($sStr)-1
        $i = Mod(($i + 1), 256)
        $j = Mod(($j + $s[$i]), 256)
        $x = $s[$i]
        $s[$i] = $s[$j]
        $s[$j] = $x
        $res &= Chr(BitXOR(Asc(StringMid($sStr, $y+1, 1)), ($s[Mod(($s[$i] + $s[$j]), 256)])))
    Next
    Return $res
EndFunc

Note that since rc4 is dual-way, doing rc4("same key here", "encrypted string") will get back your decrypted string (there's no need to a separated decrypt function).

My stuff

  Reveal hidden contents

 

Posted (edited)

the code at RC4 on win98, ws2003, win10 is compatible with yours.

Also tested time of execution on different data length and complexity, and on circumstances is faster and others slower, but generally your's is faster. Also your looks neater :)

Edited by argumentum
compared time execution

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

  • 3 weeks later...
Posted
Func rc4($sKey, $sStr)
    Local $s[256], $j = 0, $x, $res, $y, $i
    local $sKeyLen = StringLen($sKey)
    For $i = 0 To 255
        $s[$i] = $i
        $j = Mod(($j + $s[$i] + Asc(StringMid($sKey, Mod($i, $sKeyLen)+1, 1))), 256)
        $x = $s[$i]
        $s[$i] = $s[$j]
        $s[$j] = $x
    Next
    $i = 0
    $j = 0
    local $sStrLen = StringLen($sStr)-1
    For $y = 0 To $sStrLen
        $i = Mod(($i + 1), 256)
        $j = Mod(($j + $s[$i]), 256)
        $x = $s[$i]
        $s[$i] = $s[$j]
        $s[$j] = $x
        $res &= Chr(BitXOR(Asc(StringMid($sStr, $y+1, 1)), ($s[Mod(($s[$i] + $s[$j]), 256)])))
    Next
    Return $res
EndFunc

The above is some small speed modifications, untested.

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
×
×
  • Create New...