#!/usr/bin/perl

#<ENC>27</ENC>

package frc4;

#### EDIT HERE -- FOR WINDOWS/IIS BASED INSTALLATIONS ONLY #######

$config_cgi = "config.cgi"; ## <-- CHANGE THIS LINE TO THE FULL SERVER PATH TO config.cgi

# THE PATH ON A WINDOWS INSTALLATION WILL LOOK SOMETHING LIKE THIS:
# $config_cgi = "c:/inetpub/webpub/cgi-bin/pseek/config.cgi";

#### DO NOT CHANGE ANYTHING BELOW THIS LINE #################

#use FindBin;
#use lib $FindBin::Bin;

require Exporter;

use vars qw/@ISA @EXPORT @EXPORT_OK $copyright $prgname/;

@ISA = qw(Exporter);

@EXPORT = qw(RC4
				 string2hex
				 hex2string); 

@EXPORT_OK = qw($RC4
					 $retval);


################################################################################


sub RC4 {
    my($message, $key) = @_;


        my($RC4);
        my(@asciiary);
        my(@keyary);
        my($index,$jump,$temp,$y,$t,$x,$keylen);

        if (length($key) == 0) { exit; }
        if (length($message) == 0) { exit; }


        $keylen = length($key);
        for ($index = 0; $index <= 255; $index++) {
              $keyary[$index] = ord(substr($key, ($index%$keylen) + 1, 1));
        }

        for ($index = 0; $index <= 255; $index++) {
              $asciiary[$index] = $index;
        }

        $jump = 0;

        for ($index = 0; $index <= 255; $index++) {

            $jump = ($jump + $asciiary[$index] + $keyary[$index])%256;
            $temp = $asciiary[$index];
            $asciiary[$index] = $asciiary[$jump];
            $asciiary[$jump] = $temp;
        }

        $index = 0;
        $jump = 0;

        for ($x = 0; $x < length($message); $x++) {
            $index = ($index + 1)%256;
            $jump = ($jump + $asciiary[$index])%256;

            $t = ($asciiary[$index] + $asciiary[$jump])%256;

            $temp = $asciiary[$index];
            $asciiary[$index] = $asciiary[$jump];
            $asciiary[$jump] = $temp;

            $y = $asciiary[$t];

            $RC4 .= chr(ord(substr($message, $x, 1))^$y);
        }
        return($RC4);
}

sub string2hex {
    my($instring) = @_;
    my($retval,$strlen,$posx,$tval,$h1,$h2);
    my(@hexvals) = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');

    $strlen = length($instring);
    for ($posx = 0; $posx < $strlen; $posx++) {
          $tval = ord(substr($instring,$posx,1));
          $h1 = int($tval/16);
          $h2 = int($tval - ($h1*16));
          $retval .= $hexvals[$h1] . $hexvals[$h2];
    }
    return($retval);
}

sub hex2string {
    my($instring) = @_;
    my($retval,$strlen,$posx);
    $strlen = length($instring);
    for ($posx = 0; $posx < $strlen; $posx=$posx+2) {
          $retval .= chr(hex(substr($instring,$posx,2)));
    }
    return($retval);
}