<?php

/*
 * RC4 implimentation in PHP
 * Copyright: 2001, Paul Gregg <pgregg@pgregg.com>
 * 
 * http://www.pgregg.com/projects/php/code/rc4.inc.phps
 * Example use: http://www.pgregg.com/code/rc4.php
 *
 */

Function RC4(&$pStrMessage, &$pStrKey) {

    
$lBytAsciiAry array_fill(02560);
    
$lBytKeyAry array_fill(02560);
    
$lLngIndex 0;
    
$lBytJump 0;
    
$lBytTemp '';
    
$lBytY 0;
    
$lLngT 0;
    
$lLngX 0;
    
$lLngKeyLength 0;
    
    
// Validate data
    
if (strlen($pStrKey) == 0) return; 
    if (
strlen($pStrMessage) == 0) return;

    
// transfer repeated key to array and Initialize S
    
$lLngKeyLength strlen($pStrKey);
    
    for(
$lLngIndex=0$lLngIndex<256$lLngIndex++) {
        
$lBytKeyAry[$lLngIndex] = ord(substr($pStrKey, ( $lLngIndex $lLngKeyLength) , 1));
        
$lBytAsciiAry[$lLngIndex] = $lLngIndex;
    }
    
    
    
// Switch values of S around based off of index and Key value
    
$lBytJump 0;
    for(
$lLngIndex=0$lLngIndex<256$lLngIndex++) {
    
            
// Figure index to switch
        
$lBytJump = ($lBytJump $lBytAsciiAry[$lLngIndex] + $lBytKeyAry[$lLngIndex]) % 256;
        
        
// Do the switch
        
$lBytTemp $lBytAsciiAry[$lLngIndex];
        
$lBytAsciiAry[$lLngIndex]    = $lBytAsciiAry[$lBytJump];
        
$lBytAsciiAry[$lBytJump]    = $lBytTemp;
        
    }

    
    
$lLngIndex 0;
    
$lBytJump 0;
    for(
$lLngX 0$lLngX strlen($pStrMessage);  $lLngX++) {
        
$lLngIndex = ($lLngIndex 1) % 256// ' wrap index
        
$lBytJump = ($lBytJump $lBytAsciiAry[$lLngIndex]) % 256// ' wrap J+S()
        
            // Add/Wrap those two        
        
$lLngT = ($lBytAsciiAry[$lLngIndex] + $lBytAsciiAry[$lBytJump]) % 256;
        
        
// Switcheroo
        
$lBytTemp $lBytAsciiAry[$lLngIndex];
        
$lBytAsciiAry[$lLngIndex]    = $lBytAsciiAry[$lBytJump];
        
$lBytAsciiAry[$lBytJump]    = $lBytTemp;

        
$lBytY $lBytAsciiAry[$lLngT];
    
            
// Character Encryption ...    
        
$pStrMessage[$lLngX] = chr(ord(substr($pStrMessage$lLngX1)) ^ $lBytY);
        
flush();
    }
}

?>