Jump to content
Jefrey

RC4 (PHP/JS compatible)

Recommended Posts

Jefrey

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).

  • Like 2

My stuff

Spoiler

My UDFs  _AuThread multithreading emulation for AutoIt · _ExtInputBox an inputbox with multiple inputs and more features · forceUTF8 fix strings encoding without knowing its original charset · JSONgen JSON generator · _TCPServer UDF multi-client and multi-task (run on background) event-based TCP server easy to do · _TCPClient_UDF multi-server and multi-task (runs on background) event-based TCP client easy to do · ParseURL and ParseStr functions ported from PHP · _CmdLine UDF easily parse command line parameters, keys or flags · AutoPHP Create documents (bills, incomes) from HTML by sending variables/arrays from AutoIt to PHP · (Un)Serialize Convert arrays and data into a storable string (PHP compatible) · RTTL Plays and exports to MP3 Nokia-format monophonic ringtones (for very old cellphones) · I18n library Simple and easy to use localization library · Scripting.Dictionary OOP and OOP-like approach · Buffer/stack limit arrays to N items by removing the last one once the limit is reached · NGBioAPI UDF to work with Nitgen fingerprint readers · Serial/Licensing system require license key based on unique machine ID from your users · HTTP a simple WinHTTP library that allows GET, POST and file uploads · Thread true AutoIt threads (under-dev) · RC4 RC4 encryption compatible with PHP and JS Classes _WKHtmlToX uses wkhtmlto* to convert HTML files and webpages into PDF or images (jpg, bmp, gif, png...) Snippets _Word_DocFindReplaceByLongText replace strings using Word UDF with strings longer than 255 characters (MSWord limit) rangeparser parser for printing-like pages interval (e.g.: "1,2,3-5") EnvParser parse strings/paths with environment variables and get full path Random stuff Super Mario beep sound your ears will hurt

 

Share this post


Link to post
Share on other sites
argumentum

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

Share this post


Link to post
Share on other sites
TouchOdeath
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.

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

×