Page 1 of 1

looking for a simple encryption/decryption algorithm

Posted: Thu Jan 19, 2006 2:24 am
by jasongr
Hi

I am looking for an alogrithm that will allow me to perform encryption/decryption
based on a hidden key.
I am preferring one that comes built in in PHP and not one that I need to start including
extensions for

any suggestions?

Posted: Thu Jan 19, 2006 2:29 am
by Chris Corbyn

Posted: Thu Jan 19, 2006 2:31 am
by jasongr
Hi d11wtq

thanks for the reply, however, mcrypt requires re-compiling PHP with --with-mcrypt[=DIR] parameter

I am looking for a solution that doesn't require any compilation. Is there such?

Posted: Thu Jan 19, 2006 3:43 am
by AKA Panama Jack
Here you go...

Code: Select all

define ("OrdKey","Make this key anything you want."); 

function ord_crypt_encode($data) {
	$result = '';
	for($i=0; $i<strlen($data); $i++) {
		$char = substr($data, $i, 1);
		$keychar = substr(OrdKey, ($i % strlen(OrdKey))-1, 1);
		$char = chr(ord($char)+ord($keychar));
		$result.=$char;
	}
	return bin2hex($result); 
}

function ord_crypt_decode($data) {
	$result = '';
	$data =  @pack("H" . strlen($data), $data); 
	for($i=0; $i<strlen($data); $i++) {
		$char = substr($data, $i, 1);
		$keychar = substr(OrdKey, ($i % strlen(OrdKey))-1, 1);
		$char = chr(ord($char)-ord($keychar));
		$result.=$char;
	}
	return $result;
}

Posted: Thu Jan 19, 2006 4:25 am
by redmonkey
I'm not sure there is much else (if anything) built into PHP if mcrypt isn't an option. In which case i'd say RC4, it's relatively secure and relatively light weight.