A Reversible Encryption Routine for PHP
by Tony MarstonTonyMarston.net
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;
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
$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++) {
$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
$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
// 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;
$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
// 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
// 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
// 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;
$target .= $char1;
Finally we close the for loop and return the decrypted string.
} // for
return rtrim($target);
} // decrypt
return rtrim($target);
} // decrypt
Options:
Printer Friendly
Email Friend
About The Author:
I have been a software engineer, both designing and developing, since 1977. I have worked with a variety of 2nd, 3rd and 4th generation languages on a mixture of mainframes, mini- and micro-computers. I have worked with flat files, indexed files, hierarchical databases, network databases and relational databases. The user interfaces have included punched card, paper tape, teletype, block mode, CHUI, GUI and web. I have written code which has been procedural, model-driven, event-driven, component-based and object oriented. I have built software using the 1-tier, 2-tier, 3-tier and Model-View-Controller (MVC) architectures. After working with COBOL for 16 years I switched to UNIFACE in 1993, starting with version 5, then progressing through version 6 to version 7. In the middle of 2002 I decided to teach myself to develop web applications using PHP and MySQL.
I have been a software engineer, both designing and developing, since 1977. I have worked with a variety of 2nd, 3rd and 4th generation languages on a mixture of mainframes, mini- and micro-computers. I have worked with flat files, indexed files, hierarchical databases, network databases and relational databases. The user interfaces have included punched card, paper tape, teletype, block mode, CHUI, GUI and web. I have written code which has been procedural, model-driven, event-driven, component-based and object oriented. I have built software using the 1-tier, 2-tier, 3-tier and Model-View-Controller (MVC) architectures. After working with COBOL for 16 years I switched to UNIFACE in 1993, starting with version 5, then progressing through version 6 to version 7. In the middle of 2002 I decided to teach myself to develop web applications using PHP and MySQL.
