Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

A Reversible Encryption Routine for PHP

By Tony Marston
2005-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) 

{
if (empty($key)) {
$this->errors[] = 'No value has been supplied for the encryption key';
return;
} // if
The first entry in the array is the length of the $key string.
$array[] = strlen($key);
Next we set up a for loop to examine every character in the $key string.
$tot = 0;  

for ($i = 0; $i < strlen($key); $i++) {
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.
 $char = substr($key, $i, 1); 

$num = strpos($this->scramble1, $char);
if ($num === false) {
$this->errors[] = "Key contains an invalid character ($char)";
return;
} // if
He we append the number to the output array and accumulate the total for later.


$array[] = $num;
$tot = $tot + $num;
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.

} // 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


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1