Your leaking thatched hut during the restoration of a pre-Enlightenment state.

 

Hello, my name is Judas Gutenberg and this is my blaag (pronounced as you would the vomit noise "hyroop-bleuach").



links

decay & ruin
Biosphere II
Chernobyl
dead malls
Detroit
Irving housing

got that wrong
Paleofuture.com

appropriate tech
Arduino μcontrollers
Backwoods Home
Fractal antenna

fun social media stuff


Like asecular.com
(nobody does!)

Like my brownhouse:
   new games for myself
Friday, June 11 2004
I love PHP as a backend web programming language. It's become completely natural for me, and I'm increasingly coming to the view that my huge body of VBScript ASP code is orphaned forever in a hermetic, proprietary world, increasingly cut off from the future by Microsoft's manipulations of the ground beneath it. These are concerns one doesn't have when the ground on which you build your code is free and open and built on other code that is free and open, all the way down like that stack of turtles that hold up the world.
For some reason I just assumed that PHP had built-in functions for two-way encryption, an important capability when you need to ensure the security of data sent over insecure channels (particularly query strings). But PHP doesn't; it only has built-in functions for one-way encryption, which is great for verifying identity but not for sending data. I did a Google search looking for some simple examples of two-way encryption done entirely in PHP, but I came up empty-handed.
So today, in relatively short time, I built a rather robust set of functions for encrypting and decrypting an arbitrary string of data. The encrypted data produced by the encryption function is a series of hexadecimal pairs, which is an especially trouble-free format for sending data using query strings. But I wanted to make a more compressed form of encrypted data, one using, say, all the letters of the alphabet. I thought maybe I could just upper-case the hexadecimal data and then replace certain pairs of digits in the encrypted data with lower-case letters, but something kept screwing up my algorithms. Eventually I just gave up - what I was trying to achieve would have provided such a minor benefit that it didn't seem worth the effort. It was one of the rare times when I actually abandoned an algorithmic attempt. Usually I see these programming difficulties as puzzles that must be solved, by sheer force of will if necessary. It's a great intellectual exercise and always results in enormous satisfaction when completed. But I don't have enough hours in my days as it is without making up new games for myself.
For those arriving on this page as a result of a Google search, I will now provide the satisfaction you are craving (a satisfaction I too once craved):

function AddCharacters($chrOne, $chrTwo)
{
	$intOne=ord($chrOne);
	$intTwo=ord($chrTwo);
	$intRollCombo=$intOne + $intTwo;
	if ($intRollCombo>255)
	{
		$intRollCombo=$intRollCombo-256;
	}
	return $intRollCombo;
}

function SubtractCharacters($chrOne, $chrTwo)
{
	$intOne=ord($chrOne);
	$intTwo=ord($chrTwo);
	$intRollCombo=$intOne - $intTwo;
	if ($intRollCombo<0)
	{
		$intRollCombo=256+$intRollCombo;
	}
	return $intRollCombo;
}


function CombineStrings($strOne, $strTwo, $strOperation, $strType)
{
	$out="";
	$intLenOne=strlen($strOne);
	$intLenTwo=strlen($strTwo);
	$strNonDec="";
	if ($strType=="fromhex")
	{
		for ($i=0; $i<$intLenOne; $i=$i+2)
		{
			$strPair=substr($strOne, $i, 2);
			$strNonDec.=chr(hexdec($strPair));
		}
		$strOne=$strNonDec;
	}
	$intLenOne=strlen($strOne);
	if ($strTwo=="")
	{
		$strTwo=" ";
	}
	if ($intLenOne>$intLenTwo)
	{
		while (strlen($strTwo)<$intLenOne)
		{
			$strTwo.=$strTwo;
		}
	}
	$strTwo=substr($strTwo, 0, $intLenOne-1);
	for ($i=0; $i<$intLenOne; $i++)
	{
		$chrThisOne=substr($strOne, $i, 1);
		$chrThisTwo=substr($strTwo, $i, 1);
		if ($strOperation=="add")
		{
			$chrSum=chr(AddCharacters($chrThisOne, $chrThisTwo));
		}
		else if ($strOperation=="subtract")
		{
			$chrSum=chr(SubtractCharacters($chrThisOne, $chrThisTwo));
		}
		if ($strType=="tohex")
		{
			$chrSum=str_pad(dechex(ord($chrSum)), 2, "0", STR_PAD_LEFT);
		}
		$out.=$chrSum;
	}
	return $out;
}

function AddStrings($strOne, $strTwo)
{
	return CombineStrings($strOne, $strTwo, "add", "");
}

function SubtractStrings($strOne, $strTwo)
{
	return CombineStrings($strOne, $strTwo, "subtract", "");
}

function Encrypto($strIn, $strKey)
{
	$out= strtoupper(CombineStrings($strIn, $strKey, "add", "tohex"));
	//$out=Compress($out);
	return $out;
}

function Decrypto($strIn, $strKey)
{
	//$strIn=Decompress($strIn);
	$out=CombineStrings($strIn, $strKey, "subtract", "fromhex");
	return $out;
}


For linking purposes this article's URL is:
http://asecular.com/blog.php?040611

feedback
previous | next