<?php
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

define('RC4PHRASE', '100221yaMDA42');
	/****************************************************************
	* filename: cryptroutine.php 
	* author(s): Jonathan Haase
	*
	* $Id: cryptroutine.php,v 1.2 2002/04/25 14:00:21 jlhaase Exp $
	*
	* Description: This file contains the functions EncryptPass and 
	* DecryptPass.  Their purpose to provide some minor scrambling
	* to avoid storing passwords in clear-text in php sessions files.
	*
	*****************************************************************/
class cryptRoutineObj {
	// The sewString function will insert random characters into the string
	// at the given interval.
	public function sewString($string, $thread_len, $interval) 
	{
		$string_len = strlen($string);
	
		$outString_len = $string_len + intval($thread_len * ($string_len / $interval)) - 1;
		$i = $interval;
		while ($outString_len > $i )
		{
			$thread = '';
			while (strlen($thread) < $thread_len) {
				srand((double)microtime()*1000000);
				$thread .= Chr(rand(97, 122));
			}
			$string = substr($string, 0, $i) . $thread . substr($string, $i);
			$i = $i + $interval + $insert_len + 1;
			
		}
		return $string;
	}
	// unsewString will remove the random characters inserted with sewString
	public function unsewString($string, $thread_len, $interval)
	{
		$string_len = strlen($string);
		$outString_len = intval(($interval/($interval + $thread_len) * $string_len) + ($interval / ($interval + $thread_len)));
		$i = $interval;
		$j = $i + $thread_len;
		while(strlen($string) > $outString_len)
		{
			$string = substr($string, 0, $i) . substr($string, $j);
			$i = $i + $interval;
			$j = $i + $thread_len;
		}
		return $string;
	} 
	// This function provides some minor password scrambling. 
	public function EncryptPass($password)
	{
		// seed the random number generator
		srand((double)microtime()*1000000); 
		// generate a random character to use in the cyphertext
		$cypherChar = Chr(rand(33,122));
		// seed the randmon number generator again
		srand((double)microtime()*1000000); 
		// get a second random character to use in the cyphertext
		$cypherChar2 = Chr(rand(33,122));		
		// reverse the password string.
	     $drowssap = strrev($password);		
	 	$length = strlen($drowssap);			
		// generate a blank string the same length as the password
   		$cypherstring = str_pad("", $length, Chr(255));	
		// XOR the blank string with the password
	 	$drowssap = $drowssap ^ $cypherstring;		
		// generate a string of *'s, the same length as the password		
	 	$cypherstring = str_pad("", $length, Chr(42));	
		// XOR this string with the password
	 	$drowssap = $drowssap ^ $cypherstring;			
		// generate a string using the first random character.
		$cypherstring = str_pad("", $length, $cypherChar); 
		// XOR the cypherstring with the password and add the cypherchar to the
		// beginning of the password.	
		$drowssap = $cypherChar . ($drowssap ^ $cypherstring); 
		// generate a string using the second random cypherchar
		$cypherstring = str_pad("", $length + 1, $cypherChar2);
		// XOR the cypherstring with the password and add the second cypherchar
		// to the beginning of the password.
		$drowssap = $cypherChar2 . ($drowssap ^ $cypherstring);
		// return the cyphertext password
    	return $drowssap;
	}
	public function DecryptPass($drowssap)
	{
	 	// Grab the first cypherchar from the beginning of the string
	 	$cypherChar = substr($drowssap, 0, 1);
		// Assign the remainder of the string back to the cyphertext password
	 	$drowssap = substr($drowssap, 1);
		$length = strlen($drowssap);
		// Generate a string the same length as the cyphertext using the first cypherchar
		$cypherstring = str_pad("", $length, $cypherChar);
		// XOR the cypherstring with the cyphertext
		$drowssap = $drowssap ^ $cypherstring;
		// Get the second cypherchar from the beginning of the string
	 	$cypherChar = substr($drowssap, 0, 1);
		// Assign the remainder of the string back to the cyphertext password
	 	$drowssap = substr($drowssap, 1);
		$length = strlen($drowssap);
		// Generate a string the same length as the cyphertext using the second cypherchar
		$cypherstring = str_pad("", $length, $cypherChar);
		// XOR the cypherstring with the cyphertext
		$drowssap = $drowssap ^ $cypherstring;
		// Generate a cypherstring using *
		$cypherstring = str_pad("", $length, Chr(42));
		// XOR the cypherstring with the cyphertext
		$passwrd = $drowssap ^ $cypherstring; 
		// Generate a blank cypherstring
	 	$cypherstring = str_pad("", $length, Chr(255));
		// XOR the cypherstring with the cyphertext
	 	$passwrd = $passwrd ^ $cypherstring;
		// Now reverse the cypher and you have the password
	 	$passwrd = strrev($passwrd);
		// Return the cleartext password.
		return $passwrd;
	}	  
	
/***************************************************************************
*	The rc4 encryption routine is included so that we can use it for saving
*	the password and username in a cookie on the users machine for cross
*	server login functionality.....
***************************************************************************/

	public function rc4crypt($data, $pass)	
	{
		$pwd_length = strlen($pass);
		
		// Create the key, using the given password...
		for($i=0; $i < 255; $i++) {
			$key[$i] = ord(substr($pass, ($i % $pwd_length)+1, 1));
			$box[$i] = $i;
		}
		for($i=0; $i < 255; $i++) {
			$x = ($x + $box[$i] + $key[$i]) % 256;
			$temp_swap = $box[$i];
			$box[$i] = $box[$x];
			$box[$x] = $temp_swap;
		}

		// Now actually encrypt the data...
		$a = 0;
		$j = 0;
		$cipher = "";
		for($i=0; $i < strlen($data); $i++) {
			$a = ($a +1) % 256;
			$j = ($j + $box[$a]) %256;
			$temp = $box[$a];
			$box[$a] = $box[$j];
			$box[$j] = $temp;
			$k = $box[(($box[$a] + $box[$j]) % 256)];
			$cipherby = ord(substr($data, $i, 1)) ^ $k;
			$cipher .= chr($cipherby);
		}
		return $cipher;
	}	
	/************************************************************************
	*	decodeFromHeader is used to fix funkty From Headers by Microsoft clients
	*************************************************************************/
	public function decodeFromHeader($from) 
	{
		if(ereg('=\?.{0,}\?[Bb]\?',$from))
		{
			$arrHead=split('=\?.{0,}\?[Bb]\?',$from);
			while(list($key,$value)=each($arrHead))
			{
				if(ereg('\?=',$value)){
					$arrTemp=split('\?=',$value);
					$arrTemp[0]=base64_decode($arrTemp[0]);
					$arrHead[$key]=join('',$arrTemp);
				}
			}
			$from=join('',$arrHead);
		} elseif (ereg('=\?.{0,}\?[Qq]\?',$from))
		{
			$from=quoted_printable_decode($from);
			$from=ereg_replace('=\?.{0,}\?Q\?','',$from);
			$from=ereg_replace('\?=','',$from);
		}
		return $from;
	}
}
?>
