A Reversible Encryption Routine for PHP

by Tony Marston
TonyMarston.net

The ApplyFudgeFactor function

This function will return an adjustment value using the contents of the $fudgefactor array. Note that $fudgefactor is passed by reference so that it can be modified.

   function _applyFudgeFactor (&$fudgefactor) 
{


Here we extract the first entry in the array and remove it from the array.

$fudge = array_shift($fudgefactor);


Next we add in the optional $adj value and put the result back into the end of the array.

$fudge = $fudge + $this->adj;
$fudgefactor[] = $fudge;


If a $modulus value has been supplied we use it and possibly reverse the sign on the output value.

if (!empty($this->mod)) {                   // if modifier has been supplied 
if ($fudge % $this->mod == 0) {  // if it is divisible by modifier 
$fudge = $fudge * -1;            // reverse then sign 
} // if 
} // if


There is no more 'fudging' left to do, so we can return the value to the calling process.

    return $fudge; 

} // _applyFudgeFactor

The CheckRange Function

This function checks that the value in $num can actually be used as a pointer to an entry in $scramble1.

      function _checkRange ($num) 
{


Indexing starts at 0, not 1, so we must subtract 1 from string length.

          $limit = strlen($this->scramble1)-1;


If the value is too high we must reduce it.

      while ($num > $limit) { 
$num = $num - $limit; 
} // while


If the value is too low we must increase it.

      while ($num < 0) { 
$num = $num + $limit; 
} // while


We can now return a valid pointer back to the calling process.

      return $num; 

} // _checkRange



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.

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network