| [ Index ] | PHP Cross Reference of Mambo 4.6.5 |
|
| [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 // ****************************************************************************** 3 // A reversible password encryption routine by: 4 // Copyright 2003-2007 by A J Marston <http://www.tonymarston.net> 5 // Distributed under the GNU General Public Licence 6 // Modification: May 2007, M. Kolar <http://mkolar.org>: 7 // No need for repeating the first character of scramble strings at the end; 8 // instead using the exact inverse function transforming $num2 to $num1. 9 // ****************************************************************************** 10 11 class encryption_class { 12 13 var $scramble1; // 1st string of ASCII characters 14 var $scramble2; // 2nd string of ASCII characters 15 16 var $errors; // array of error messages 17 var $adj; // 1st adjustment value (optional) 18 var $mod; // 2nd adjustment value (optional) 19 20 // **************************************************************************** 21 // class constructor 22 // **************************************************************************** 23 function encryption_class () 24 { 25 $this->errors = array(); 26 27 // Each of these two strings must contain the same characters, but in a different order. 28 // Use only printable characters from the ASCII table. 29 // Do not use single quote, double quote or backslash as these have special meanings in PHP. 30 // Each character can only appear once in each string. 31 $this->scramble1 = '! #$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'; 32 $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'; 33 34 if (strlen($this->scramble1) <> strlen($this->scramble2)) { 35 trigger_error('** SCRAMBLE1 is not same length as SCRAMBLE2 **', E_USER_ERROR); 36 } // if 37 38 $this->adj = 1.75; // this value is added to the rolling fudgefactors 39 $this->mod = 3; // if divisible by this the adjustment is made negative 40 41 } // constructor 42 43 // **************************************************************************** 44 function decrypt ($key, $source) 45 // decrypt string into its original form 46 { 47 $this->errors = array(); 48 49 // convert $key into a sequence of numbers 50 $fudgefactor = $this->_convertKey($key); 51 if ($this->errors) return; 52 53 if (empty($source)) { 54 $this->errors[] = 'No value has been supplied for decryption'; 55 return; 56 } // if 57 58 $target = null; 59 $factor2 = 0; 60 61 for ($i = 0; $i < strlen($source); $i++) { 62 // extract a character from $source 63 $char2 = substr($source, $i, 1); 64 65 // identify its position in $scramble2 66 $num2 = strpos($this->scramble2, $char2); 67 if ($num2 === false) { 68 $this->errors[] = "Source string contains an invalid character ($char2)"; 69 return; 70 } // if 71 72 // get an adjustment value using $fudgefactor 73 $adj = $this->_applyFudgeFactor($fudgefactor); 74 75 $factor1 = $factor2 + $adj; // accumulate in $factor1 76 $num1 = $num2 - round($factor1); // generate offset for $scramble1 77 $num1 = $this->_checkRange($num1); // check range 78 $factor2 = $factor1 + $num2; // accumulate in $factor2 79 80 // extract character from $scramble1 81 $char1 = substr($this->scramble1, $num1, 1); 82 83 // append to $target string 84 $target .= $char1; 85 86 //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n"; 87 88 } // for 89 90 return rtrim($target); 91 92 } // decrypt 93 94 // **************************************************************************** 95 function encrypt ($key, $source, $sourcelen = 0) 96 // encrypt string into a garbled form 97 { 98 $this->errors = array(); 99 100 // convert $key into a sequence of numbers 101 $fudgefactor = $this->_convertKey($key); 102 if ($this->errors) return; 103 104 if (empty($source)) { 105 $this->errors[] = 'No value has been supplied for encryption'; 106 return; 107 } // if 108 109 // pad $source with spaces up to $sourcelen 110 while (strlen($source) < $sourcelen) { 111 $source .= ' '; 112 } // while 113 114 $target = null; 115 $factor2 = 0; 116 117 for ($i = 0; $i < strlen($source); $i++) { 118 // extract a character from $source 119 $char1 = substr($source, $i, 1); 120 121 // identify its position in $scramble1 122 $num1 = strpos($this->scramble1, $char1); 123 if ($num1 === false) { 124 $this->errors[] = "Source string contains an invalid character ($char1)"; 125 return; 126 } // if 127 128 // get an adjustment value using $fudgefactor 129 $adj = $this->_applyFudgeFactor($fudgefactor); 130 131 $factor1 = $factor2 + $adj; // accumulate in $factor1 132 $num2 = round($factor1) + $num1; // generate offset for $scramble2 133 $num2 = $this->_checkRange($num2); // check range 134 $factor2 = $factor1 + $num2; // accumulate in $factor2 135 136 // extract character from $scramble2 137 $char2 = substr($this->scramble2, $num2, 1); 138 139 // append to $target string 140 $target .= $char2; 141 142 //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n"; 143 144 } // for 145 146 return $target; 147 148 } // encrypt 149 150 // **************************************************************************** 151 function getAdjustment () 152 // return the adjustment value 153 { 154 return $this->adj; 155 156 } // setAdjustment 157 158 // **************************************************************************** 159 function getModulus () 160 // return the modulus value 161 { 162 return $this->mod; 163 164 } // setModulus 165 166 // **************************************************************************** 167 function setAdjustment ($adj) 168 // set the adjustment value 169 { 170 $this->adj = (float)$adj; 171 172 } // setAdjustment 173 174 // **************************************************************************** 175 function setModulus ($mod) 176 // set the modulus value 177 { 178 $this->mod = (int)abs($mod); // must be a positive whole number 179 180 } // setModulus 181 182 // **************************************************************************** 183 // private methods 184 // **************************************************************************** 185 function _applyFudgeFactor (&$fudgefactor) 186 // return an adjustment value based on the contents of $fudgefactor 187 // NOTE: $fudgefactor is passed by reference so that it can be modified 188 { 189 $fudge = array_shift($fudgefactor); // extract 1st number from array 190 $fudge = $fudge + $this->adj; // add in adjustment value 191 $fudgefactor[] = $fudge; // put it back at end of array 192 193 if (!empty($this->mod)) { // if modifier has been supplied 194 if ($fudge % $this->mod == 0) { // if it is divisible by modifier 195 $fudge = $fudge * -1; // make it negative 196 } // if 197 } // if 198 199 return $fudge; 200 201 } // _applyFudgeFactor 202 203 // **************************************************************************** 204 function _checkRange ($num) 205 // check that $num points to an entry in $this->scramble1 206 { 207 $num = round($num); // round up to nearest whole number 208 209 $limit = strlen($this->scramble1); 210 211 while ($num >= $limit) { 212 $num = $num - $limit; // value too high, so reduce it 213 } // while 214 while ($num < 0) { 215 $num = $num + $limit; // value too low, so increase it 216 } // while 217 218 return $num; 219 220 } // _checkRange 221 222 // **************************************************************************** 223 function _convertKey ($key) 224 // convert $key into an array of numbers 225 { 226 if (empty($key)) { 227 $this->errors[] = 'No value has been supplied for the encryption key'; 228 return; 229 } // if 230 231 $array[] = strlen($key); // first entry in array is length of $key 232 233 $tot = 0; 234 for ($i = 0; $i < strlen($key); $i++) { 235 // extract a character from $key 236 $char = substr($key, $i, 1); 237 238 // identify its position in $scramble1 239 $num = strpos($this->scramble1, $char); 240 if ($num === false) { 241 $this->errors[] = "Key contains an invalid character ($char)"; 242 return; 243 } // if 244 245 $array[] = $num; // store in output array 246 $tot = $tot + $num; // accumulate total for later 247 } // for 248 249 $array[] = $tot; // insert total as last entry in array 250 251 return $array; 252 253 } // _convertKey 254 255 // **************************************************************************** 256 } // end encryption_class 257 // **************************************************************************** 258 259 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed May 23 00:05:01 2012 | Cross-referenced by PHPXref 0.7 |
| Mambo API: Mambo is Free software released under the GNU/General Public License, Version 2 |