A Reversible Encryption Routine for PHP
By Tony Marston2005-04-15
The ConvertKey function
This function converts the $key string into an array of numbers. The first check is to ensure that a value has actually been supplied.
function _convertKey ($key)The first entry in the array is the length of the $key string.
{
if (empty($key)) {
$this->errors[] = 'No value has been supplied for the encryption key';
return;
} // if
$array[] = strlen($key);Next we set up a for loop to examine every character in the $key string.
$tot = 0;Here we extract the next character from $key and identify its position in $scramble1. Note that we cannot continue processing if the character cannot be found.
for ($i = 0; $i < strlen($key); $i++) {
$char = substr($key, $i, 1);He we append the number to the output array and accumulate the total for later.
$num = strpos($this->scramble1, $char);
if ($num === false) {
$this->errors[] = "Key contains an invalid character ($char)";
return;
} // if
At the end of the for loop we add the accumulated total to the end of the array and return the array to the calling process.
$array[] = $num;
$tot = $tot + $num;
} // for
$array[] = $tot;
return $array;
} // _convertKey
Tutorial Pages:
» A Reversible Encryption Routine for PHP
» Rotating each character a fixed number of positions
» Swapping between two different character strings
» Manipulating the index number between the two strings
» Customising the Encryption Algorithm
» Class Variables
» Class Constructor
» The ConvertKey function
» The ApplyFudgeFactor function
» The CheckRange Function
» The Decrypt function
» Using the Encryption Class
» Summary
