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 Decrypt function

This function will take an encrypted string and turn it into plain text using the supplied key. Note that this must be exactly the same key that was used to encrypt the string in the first place.
function decrypt ($key, $source) 

{
The first step is to convert the key into an array of numbers which I call $fudgefactor. The contents of the _convertkey function can be viewed here.
      $fudgefactor = $this->_convertKey($key); 

if ($this->errors) return;
This next piece of code checks that a source string has actually been supplied.
      if (empty($source)) { 

$this->errors[] = 'No value has been supplied for decryption';
return;
} // if
Here we are setting up a for loop to process each character from $source.
      $target  = NULL; 

$factor2 = 0;
for ($i = 0; $i < strlen($source); $i++) {
Here we extract the next character from $source and identify its position in $scramble2. Note that we cannot continue processing if the character cannot be found.
         $char2 = substr($source, $i, 1); 

$num2 = strpos($this->scramble2, $char2);
if ($num2 === false) {
$this->errors[] = "Source string contains an invalid character ($char2)";
return;
} // if
For some reason an offset of 0 produces a problem, so point to the last character in the string instead (which is a duplicate of the first).
         if ($num2 == 0) {

// use the last occurrence of this letter, not the first
$num2 = strlen($this->scramble1)-1;
} // if
Next we obtain the adjustment value from the $fudgefactor array and accumulate it in $factor1 along with the previous contents of $factor2. The contents of the _applyFudgeFactor function can be viewed here.
         $adj = $this->_applyFudgeFactor($fudgefactor); 

$factor1 = $factor2 + $adj;
Here we add $factor1 to the offset from the $scramble2 string ($num2) to give us the offset into the $scramble1 string ($num1). Note that factor may contain decimal digits, so it has to be rounded in order to supply a whole number.
         $num1    = round($factor1*-1) + $num2;     

// generate offset for $scramble1
The value at this point may be a negative number or even a large positive number, so we have to check that it can actually point to a character in the $scramble1 string. The contents of the _checkRange function can be viewed here.
         $num1    = $this->_checkRange($num1);      

// check range
As an added complication to confuse potential hackers we are also accumulating the value of $num2 and $factor1 in $factor2.
         $factor2 = $factor1 + $num2;               

// accumulate in $factor2
Here we extract a character from $scramble1 using the value in $num1 and append it to the output string.
         $char1 = substr($this->scramble1, $num1, 1); 

$target .= $char1;
Finally we close the for loop and return the decrypted string.
      } // for 


return rtrim($target);

} // decrypt


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