SHARE
TWEET

Tony Marston Radicore std.encryption.class.inc

a guest Jan 24th, 2015 1,207 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. RADICORE software copyright 2003-2005 A J MARSTON <http://www.tonymarston.net>
  4. RADICORE software copyright 2006-2008 Radicore Software Limited <http://www.radicore.org>
  5.  
  6. You do not own this software, you are simply granted a license to use it. Ownership of this software remains with the copyright holder.
  7.  
  8. This software is made available under the terms of version 3 the GNU Affero General Public License (AGPL) (http://www.gnu.org/licenses/agpl.html). If any derivative work is not also made available under the AGPL it will require a commercial license.
  9.  
  10. This software is made available at no charge. Any derivative work must also made available at no charge, otherwise it will require a commercial license.
  11.  
  12. For full details regarding the licensing structure, and the circumstances when a commercial license will be required, please refer to http://www.radicore.org/licensing.php
  13. */
  14. // ******************************************************************************
  15. // A reversible password encryption routine by:
  16. // Copyright 2003-2011 by A J Marston <http://www.tonymarston.net>
  17. // Distributed under the GNU General Public Licence
  18. // Modification: May 2007, M. Kolar <http://mkolar.org>:
  19. // No need for repeating the first character of scramble strings at the end;
  20. // instead using the exact inverse function transforming $num2 to $num1.
  21. // Modification: Jan 2009, A J Marston <http://www.tonymarston.net>:
  22. // Use mb_substr() if it is available (for multibyte characters).
  23. // ******************************************************************************
  24.  
  25. class encryption_class {
  26.  
  27.     var $scramble1;     // 1st string of ASCII characters
  28.     var $scramble2;     // 2nd string of ASCII characters
  29.  
  30.     var $errors;        // array of error messages
  31.     var $adj;           // 1st adjustment value (optional)
  32.     var $mod;           // 2nd adjustment value (optional)
  33.  
  34.     // ****************************************************************************
  35.     // class constructor
  36.     // ****************************************************************************
  37.     function encryption_class ()
  38.     {
  39.         $this->errors = array();
  40.  
  41.         // Each of these two strings must contain the same characters, but in a different order.
  42.         // Use only printable characters from the ASCII table.
  43.         // Do not use single quote, double quote or backslash as these have special meanings in PHP.
  44.         // Each character can only appear once in each string.
  45.         $this->scramble1 = '! #$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
  46.         $this->scramble2 = 'f^jAE]okIOzU[2&q1{3`h5w_794p@6s8?BgP>dFV=m D<TcS%Ze|r:lGK/uCy.Jx)HiQ!#$~(;Lt-R}Ma,NvW+Ynb*0X';
  47.  
  48.         if (strlen($this->scramble1) <> strlen($this->scramble2)) {
  49.             trigger_error('** SCRAMBLE1 is not same length as SCRAMBLE2 **', E_USER_ERROR);
  50.         } // if
  51.  
  52.         $this->adj = 1.75;  // this value is added to the rolling fudgefactors
  53.         $this->mod = 3;     // if divisible by this the adjustment is made negative
  54.  
  55.     } // constructor
  56.  
  57.     // ****************************************************************************
  58.     function decrypt ($key, $source)
  59.     // decrypt string into its original form
  60.     {
  61.         $this->errors = array();
  62.  
  63.         // convert $key into a sequence of numbers
  64.         $fudgefactor = $this->_convertKey($key);
  65.         if ($this->errors) return;
  66.  
  67.         if (empty($source)) {
  68.             $this->errors[] = 'No value has been supplied for decryption';
  69.             return;
  70.         } // if
  71.  
  72.         $target = null;
  73.         $factor2 = 0;
  74.  
  75.         for ($i = 0; $i < strlen($source); $i++) {
  76.             // extract a (multibyte) character from $source
  77.             if (function_exists('mb_substr')) {
  78.                 $char2 = mb_substr($source, $i, 1);
  79.             } else {
  80.                 $char2 = substr($source, $i, 1);
  81.             } // if
  82.  
  83.             // identify its position in $scramble2
  84.             $num2 = strpos($this->scramble2, $char2);
  85.             if ($num2 === false) {
  86.                 $this->errors[] = "Source string contains an invalid character ($char2)";
  87.                 return;
  88.             } // if
  89.  
  90.             // get an adjustment value using $fudgefactor
  91.             $adj     = $this->_applyFudgeFactor($fudgefactor);
  92.  
  93.             $factor1 = $factor2 + $adj;                 // accumulate in $factor1
  94.             $num1    = $num2 - round($factor1);         // generate offset for $scramble1
  95.             $num1    = $this->_checkRange($num1);       // check range
  96.             $factor2 = $factor1 + $num2;                // accumulate in $factor2
  97.  
  98.             // extract (multibyte) character from $scramble1
  99.             if (function_exists('mb_substr')) {
  100.                 $char1 = mb_substr($this->scramble1, $num1, 1);
  101.             } else {
  102.                 $char1 = substr($this->scramble1, $num1, 1);
  103.             } // if
  104.  
  105.             // append to $target string
  106.             $target .= $char1;
  107.  
  108.             //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n";
  109.  
  110.         } // for
  111.  
  112.         return rtrim($target);
  113.  
  114.     } // decrypt
  115.  
  116.     // ****************************************************************************
  117.     function encrypt ($key, $source, $sourcelen = 0)
  118.     // encrypt string into a garbled form
  119.     {
  120.         $this->errors = array();
  121.  
  122.         // convert $key into a sequence of numbers
  123.         $fudgefactor = $this->_convertKey($key);
  124.         if ($this->errors) return;
  125.  
  126.         if (empty($source)) {
  127.             $this->errors[] = 'No value has been supplied for encryption';
  128.             return;
  129.         } // if
  130.  
  131.         // pad $source with spaces up to $sourcelen
  132.         $source = str_pad($source, $sourcelen);
  133.  
  134.         $target = null;
  135.         $factor2 = 0;
  136.  
  137.         for ($i = 0; $i < strlen($source); $i++) {
  138.             // extract a (multibyte) character from $source
  139.             if (function_exists('mb_substr')) {
  140.                 $char1 = mb_substr($source, $i, 1);
  141.             } else {
  142.                 $char1 = substr($source, $i, 1);
  143.             } // if
  144.  
  145.             // identify its position in $scramble1
  146.             $num1 = strpos($this->scramble1, $char1);
  147.             if ($num1 === false) {
  148.                 $this->errors[] = "Source string contains an invalid character ($char1)";
  149.                 return;
  150.             } // if
  151.  
  152.             // get an adjustment value using $fudgefactor
  153.             $adj     = $this->_applyFudgeFactor($fudgefactor);
  154.  
  155.             $factor1 = $factor2 + $adj;             // accumulate in $factor1
  156.             $num2    = round($factor1) + $num1;     // generate offset for $scramble2
  157.             $num2    = $this->_checkRange($num2);   // check range
  158.             $factor2 = $factor1 + $num2;            // accumulate in $factor2
  159.  
  160.             // extract (multibyte) character from $scramble2
  161.             if (function_exists('mb_substr')) {
  162.                 $char2 = mb_substr($this->scramble2, $num2, 1);
  163.             } else {
  164.                 $char2 = substr($this->scramble2, $num2, 1);
  165.             } // if
  166.  
  167.             // append to $target string
  168.             $target .= $char2;
  169.  
  170.             //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n";
  171.  
  172.         } // for
  173.  
  174.         return $target;
  175.  
  176.     } // encrypt
  177.  
  178.     // ****************************************************************************
  179.     function getAdjustment ()
  180.     // return the adjustment value
  181.     {
  182.         return $this->adj;
  183.  
  184.     } // setAdjustment
  185.  
  186.     // ****************************************************************************
  187.     function getModulus ()
  188.     // return the modulus value
  189.     {
  190.         return $this->mod;
  191.  
  192.     } // setModulus
  193.  
  194.     // ****************************************************************************
  195.     function setAdjustment ($adj)
  196.     // set the adjustment value
  197.     {
  198.         $this->adj = (float)$adj;
  199.  
  200.     } // setAdjustment
  201.  
  202.     // ****************************************************************************
  203.     function setModulus ($mod)
  204.     // set the modulus value
  205.     {
  206.         $this->mod = (int)abs($mod);    // must be a positive whole number
  207.  
  208.     } // setModulus
  209.  
  210.     // ****************************************************************************
  211.     // private methods
  212.     // ****************************************************************************
  213.     function _applyFudgeFactor (&$fudgefactor)
  214.     // return an adjustment value  based on the contents of $fudgefactor
  215.     // NOTE: $fudgefactor is passed by reference so that it can be modified
  216.     {
  217.         $fudge = array_shift($fudgefactor);     // extract 1st number from array
  218.         $fudge = $fudge + $this->adj;           // add in adjustment value
  219.         $fudgefactor[] = $fudge;                // put it back at end of array
  220.  
  221.         if (!empty($this->mod)) {               // if modifier has been supplied
  222.             if ($fudge % $this->mod == 0) {     // if it is divisible by modifier
  223.                 $fudge = $fudge * -1;           // make it negative
  224.             } // if
  225.         } // if
  226.  
  227.         return $fudge;
  228.  
  229.     } // _applyFudgeFactor
  230.  
  231.     // ****************************************************************************
  232.     function _checkRange ($num)
  233.     // check that $num points to an entry in $this->scramble1
  234.     {
  235.         $num = round($num);         // round up to nearest whole number
  236.  
  237.         $limit = strlen($this->scramble1);
  238.  
  239.         while ($num >= $limit) {
  240.             $num = $num - $limit;   // value too high, so reduce it
  241.         } // while
  242.         while ($num < 0) {
  243.             $num = $num + $limit;   // value too low, so increase it
  244.         } // while
  245.  
  246.         return $num;
  247.  
  248.     } // _checkRange
  249.  
  250.     // ****************************************************************************
  251.     function _convertKey ($key)
  252.     // convert $key into an array of numbers
  253.     {
  254.         if (empty($key)) {
  255.             $this->errors[] = 'No value has been supplied for the encryption key';
  256.             return;
  257.         } // if
  258.  
  259.         $array[] = strlen($key);    // first entry in array is length of $key
  260.  
  261.         $tot = 0;
  262.         for ($i = 0; $i < strlen($key); $i++) {
  263.             // extract a (multibyte) character from $key
  264.             if (function_exists('mb_substr')) {
  265.                 $char = mb_substr($key, $i, 1);
  266.             } else {
  267.                 $char = substr($key, $i, 1);
  268.             } // if
  269.  
  270.             // identify its position in $scramble1
  271.             $num = strpos($this->scramble1, $char);
  272.             if ($num === false) {
  273.                 $this->errors[] = "Key contains an invalid character ($char)";
  274.                 return;
  275.             } // if
  276.  
  277.             $array[] = $num;        // store in output array
  278.             $tot = $tot + $num;     // accumulate total for later
  279.         } // for
  280.  
  281.         $array[] = $tot;                // insert total as last entry in array
  282.  
  283.         return $array;
  284.  
  285.     } // _convertKey
  286.  
  287. // ****************************************************************************
  288. } // end encryption_class
  289. // ****************************************************************************
  290.  
  291. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top