<?php
// Bren2010 Encryption v.2.0
function bren2010Encryption2($message, $password) {
	/*
	Bren2010's Encryption Algorithm v.2.0
	Author: Brendan M. (http://www.bren2010.com)
	Date: January 19, 2010
	
	Actions:
		1. Use the POA to obfuscate the given password, called poaPassword.
		2. Use a cipher to encrypt the message, called ciphered.
		3. Use virgenere encryption to encrypt the ciphered, called virgenere.
		4. Use a cipher to encrypt virgenere with reversed poaPassword, called encrypted.
		5. Return encrypted.
	*/
	
	// Step one.
	$poaPassword = POA($password);
	
	// Step two.
	$cipheredArray = array();
	$pmessageArray = str_split($message, 1);
	$ppasswordArray = str_split($poaPassword, 1);
	$messageArray = array();
	$passwordArray = array();
	
	foreach($pmessageArray as $letter) {
		array_push($messageArray, ord($letter));
	}
	
	foreach($ppasswordArray as $letter) {
		array_push($passwordArray, ord($letter));
	}
	$mcount = count($messageArray) - 1;
	$pcount = count($passwordArray) - 1;
	$foo = 0;
	$bar = 0;
	
	while ($foo <= $mcount) {
		$letter = $messageArray["$foo"];
		$pletter = $passwordArray["$bar"];
		$total = $letter + $pletter;
		
		while ($total > 126) {
			$total -= 94;
		}
		
		$char = chr($total);
		array_push($cipheredArray, $char);
		
		if ($bar == $pcount) {
			$bar = 0;
		} else {
			$bar++;
		}
		$foo++;
	}
	
	$ciphered = implode($cipheredArray);
	
	// Step three.
	$enc = enc($ciphered, $poaPassword);
	
	// Step four.
	$rcipheredArray = array();
	$mvirgArray = str_split($enc, 1);
	$ppvirgArray = array_reverse(str_split($poaPassword, 1));
	$virgArray = array();
	$pvirgArray = array();
	
	foreach($mvirgArray as $letter) {
		array_push($virgArray, ord($letter));
	}
	
	foreach($ppvirgArray as $pletter) {
		array_push($pvirgArray, ord($pletter));
	}
	$mcount = count($virgArray);
	$pcount = count($pvirgArray);
	$foo = 0;
	$bar = 0;
	
	while ($foo <= $mcount) {
		$letter = $virgArray["$foo"];
		$pletter = $pvirgArray["$bar"];
		$total = $letter + $pletter;
		
		while ($total > 126) {
			$total -= 94;
		}
		
		$char = chr($total);
		array_push($rcipheredArray, $char);
		
		if ($bar == $pcount) {
			$bar = 0;
		} else {
			$bar++;
		}
		$foo++;
	}
	
	$encrypted = implode($rcipheredArray);

	// Step 5.
	return $encrypted;
}

// Bren2010 Decryption Algorithm v.2.0
function bren2010Decryption2($message, $password) {
	/*
	Bren2010's Encryption Algorithm v.2.0
	Author: Brendan M. (http://www.bren2010.com)
	Date: January 19, 2010
		
	Actions:
		1. Use the POA to obfuscate the given password, called poaPassword.
		2. Use a cipher to decrypt the message with reversed poaPassword, called rCipher.
		3. Use virgenere to decrypt rCipher, called to virgenere.
		4. Use a cipher to decrypt virgenere, called decrypted.
		5. Return decrypted
	*/

	// Step one.
	$poaPassword = POA($password);
	
	// Step two.
	$rcipheredArray = array();
	$mvirgArray = str_split($message, 1);
	$ppvirgArray = array_reverse(str_split($poaPassword, 1));
	$virgArray = array();
	$pvirgArray = array();
	
	foreach($mvirgArray as $letter) {
		array_push($virgArray, ord($letter));
	}
	
	foreach($ppvirgArray as $pletter) {
		array_push($pvirgArray, ord($pletter));
	}
	$mcount = count($virgArray) - 1;
	$pcount = count($pvirgArray) - 1;
	$foo = 0;
	$bar = 0;
	
	while ($foo <= $mcount) {
		$letter = $virgArray["$foo"];
		$pletter = $pvirgArray["$bar"];
		$total = $letter - $pletter;
		
		while ($total < 32) {
			$total += 94;
		}
		
		$char = chr($total);
		array_push($rcipheredArray, $char);
		
		if ($bar == $pcount) {
			$bar = 0;
		} else {
			$bar++;
		}
		$foo++;
	}
	
	$rCipher = implode($rcipheredArray);
	
	// Step three.
	$dec = dec($rCipher, $poaPassword);
	
	// Step four.
	$cipheredArray = array();
	$pmessageArray = str_split($dec, 1);
	$ppasswordArray = str_split($poaPassword, 1);
	$messageArray = array();
	$passwordArray = array();
	
	foreach($pmessageArray as $letter) {
		array_push($messageArray, ord($letter));
	}
	
	foreach($ppasswordArray as $letter) {
		array_push($passwordArray, ord($letter));
	}
	$mcount = count($messageArray);
	$pcount = count($passwordArray);
	$foo = 0;
	$bar = 0;
	
	while ($foo < $mcount) {
		$letter = $messageArray["$foo"];
		$pletter = $passwordArray["$bar"];
		$total = $letter - $pletter;
		
		while ($total < 32) {
			$total += 94;
		}
		
		$char = chr($total);
		array_push($cipheredArray, $char);
		
		if ($bar == $pcount) {
			$bar = 0;
		} else {
			$bar++;
		}
		$foo++;
	}
	
	$decrypted = implode($cipheredArray);
	
	// Step five.
	return $decrypted;
}

// Password Obfuscation Algorithm
function POA($password) {
	/*
	Password Obfuscation Algorithm
	Author: Brendan M. (http://www.bren2010.com)
	Date: January 19, 2010

	Actions:
		1. Add the values of all the characters in the password together, called total.
		2. Multiply the ascii value of each character by the total, called numbers.
		3. Combine the numbers into a string, called string.
		4. Get the very first number, called prime.
		5. Seperate the string into equal parts described by the prime, called array.
		6. Hash each value of the array with SHA1 and combine into string, called hash.
		7. Hash the hash with MD5, and add it to the end of the hash, called finalHash.
		8. Return final hash.
	*/
	
	// Step one.
	$letters = str_split($password, 1);
	$total = 0;
	
	foreach($letters as $letter) {
		$ascii = ord($letter);
		$total += $ascii;
	}
	
	// Step two.
	$numbers = array();
	$letters = str_split($password, 1);
	
	foreach($letters as $letter) {
		$ascii = ord($letter);
		$mult = $ascii * $total;
		
		array_push($numbers, $mult);
	}
	
	// Step three.
	$string = implode($numbers);
	
	// Step four.
	$prime = substr($string, 0, 1);
	
	// Step five.
	$array = str_split($string, $prime);
	
	// Step six.
	$hashArray = array();
	
	foreach($array as $section) {
		$hash = sha1($section);
		array_push($hashArray, $hash);
	}
	
	$hash = implode($hashArray);
	
	// Step seven.
	$fHash = md5($hash);
	$finalHash = $hash . $fHash;
	
	// Step eight.
	return $finalHash;
}

function enc($str, $key)
{
$slen = strlen($str);
$klen = strlen($key);
for ($i = 0; $i < $slen; ++$i)
{
$str[$i] = chr(ord($str[$i]) + ord($key[$i % $klen]) % 255);
}
return base64_encode($str);
}

function dec($str, $key)
{
$str = base64_decode($str);
$slen = strlen($str);
$klen = strlen($key);
for ($i = 0; $i < $slen; ++$i)
{
$tmp = (ord($str[$i]) - ord($key[$i % $klen])) % 255;
$str[$i] = ($tmp < 0) ? chr(255 + $tmp) : chr($tmp);
}
return $str;
}
?>