A Reversible Encryption Routine for PHP
By Tony Marston2005-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);This next piece of code checks that a source string has actually been supplied.
if ($this->errors) return;
if (empty($source)) {
$this->errors[] = 'No value has been supplied for decryption';
return;
} // ifHere we are setting up a for loop to process each character from $source.
$target = NULL;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.
$factor2 = 0;
for ($i = 0; $i < strlen($source); $i++) {
$char2 = substr($source, $i, 1);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).
$num2 = strpos($this->scramble2, $char2);
if ($num2 === false) {
$this->errors[] = "Source string contains an invalid character ($char2)";
return;
} // if
if ($num2 == 0) {
// use the last occurrence of this letter, not the first
$num2 = strlen($this->scramble1)-1;
} // ifNext 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);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.
$factor1 = $factor2 + $adj;
$num1 = round($factor1*-1) + $num2;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.
// generate offset for $scramble1
$num1 = $this->_checkRange($num1);As an added complication to confuse potential hackers we are also accumulating the value of $num2 and $factor1 in $factor2.
// check range
$factor2 = $factor1 + $num2;Here we extract a character from $scramble1 using the value in $num1 and append it to the output string.
// accumulate in $factor2
$char1 = substr($this->scramble1, $num1, 1);Finally we close the for loop and return the decrypted string.
$target .= $char1;
} // 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
