Location: PHPKode > scripts > Transcrypt > transcrypt/src/class.arc4.inc.php
<?php

/************************************************************************************
*  Copyright notice
* 
*  Transcrypt
*
*  Copyright (C) 2000-2005 CH Software (hide@address.com)
*  www.chsoftware.net
*  All rights reserved
*  For more information go to: 
*  www.chsoftware.net/en/useware/conditions/conditions.htm
*
*
*
*  This program is free software; you can redistribute it and/or
*  modify it under the terms of the GNU General Public License
*  as published by the Free Software Foundation; either version 2
*  of the License, or (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*  The GNU General Public License can be found at
*  http://www.gnu.org/licenses/gpl.html
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts. 
*
*************************************************************************************/

/**
 * PHP Class of the ARC 4 Algorithm (100% compatible to the RC4 Algorithm)
 *
 * Ported from Javascript to PHP by Cornelius Herzog 2004
 * Original Javascript from http://www.howtocreate.co.uk/emails/test_RC4B64_js.htm
 *
 * Revised on 21.9.2004 by Cornelius Herzog
 *
 * @author	Cornelius Herzog (hide@address.com)
 */ 

 
class ARC4 {
	var $keybox;
	var $sbox;
	var $strText;
	var $strKey;
	var $result;
	var $pwhash;
	
	function Encode() {
		$this->pwhash = md5($this->strKey);
		$this->result = base64_encode($this->_ARC4($this->strText,$this->strKey));
	}
	
	function Decode(){
		$this->pwhash = md5($this->strKey);
		$this->result = $this->_ARC4(base64_decode($this->strText),$this->strKey);
	}
	
	
	function InitializeARC4($strKey) {
		for ($a=0; $a < 256;$a++) {
			$pos = $a % strlen($strKey);
			$val = ord($strKey[$pos]);
			$this->keybox[$a] = $val % 256;
			$this->sbox[$a] = $a;
		}
		$b = 0;
		for ($a=0; $a < 256; $a++) {
			$b = ($b + $this->sbox[$a] + $this->keybox[$a]) % 256;
			$tempSwap = $this->sbox[$a];
			$this->sbox[$a] = $this->sbox[$b];
			$this->sbox[$b] = $tempSwap;
		}
	}
	
	function _ARC4($strText,$strKey) {
		$i = 0;
		$j = 0;
		$cipher = "";
		$temp = 0;
		$this->InitializeARC4($strKey);
		for ($a = 0; $a < strlen($strText); $a++) {
			$i = ($i + 1) % 256;
			$j = ($j + $this->sbox[$i]) % 256;
			$temp = $this->sbox[$i];
			$this->sbox[$i] = $this->sbox[$j];
			$this->sbox[$j] = $temp;
	
			$k = $this->sbox[($this->sbox[$i] + $this->sbox[$j]) % 256];
	
			$cipherby = ord($strText[$a]) ^ $k;
			$cipher = $cipher . chr($cipherby);
		}
		return($cipher);
	}


}









?>
Return current item: Transcrypt