PageRenderTime 68ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/std.encryption.class.inc.php

https://github.com/bejean/advanced-search-by-my-solr-server
PHP | 301 lines | 158 code | 65 blank | 78 comment | 27 complexity | 17bd3a07f3e125f68426bcf0f50dceb9 MD5 | raw file
  1. <?php
  2. // ******************************************************************************
  3. // A reversible password encryption routine by:
  4. // Copyright 2003-2009 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. // Modification: Jan 2009, A J Marston <http://www.tonymarston.net>:
  10. // Use mb_substr() if it is available (for multibyte characters).
  11. // ******************************************************************************
  12. // Sample at
  13. // http://www.tonymarston.net/php-mysql/encryption.html
  14. // http://www.tonymarston.net/php-mysql/showsource.php?file=encryption.php
  15. /*
  16. require 'std.encryption.class.inc';
  17. $crypt = new encryption_class;
  18. ini_set('session.bug_compat_warn', 0);
  19. $crypt->setAdjustment($adj);
  20. $crypt->setModulus($mod);
  21. $errors = array();
  22. $encrypt_result = $crypt->encrypt($key, $password, $pswdlen);
  23. $errors = $crypt->errors;
  24. $decrypt_result = $crypt->decrypt($key, $password);
  25. $encrypt_result = $password;
  26. $password = $decrypt_result;
  27. */
  28. class encryption_class {
  29. var $scramble1; // 1st string of ASCII characters
  30. var $scramble2; // 2nd string of ASCII characters
  31. var $errors; // array of error messages
  32. var $adj; // 1st adjustment value (optional)
  33. var $mod; // 2nd adjustment value (optional)
  34. // ****************************************************************************
  35. // class constructor
  36. // ****************************************************************************
  37. function encryption_class ()
  38. {
  39. $this->errors = array();
  40. // Each of these two strings must contain the same characters, but in a different order.
  41. // Use only printable characters from the ASCII table.
  42. // Do not use single quote, double quote or backslash as these have special meanings in PHP.
  43. // Each character can only appear once in each string.
  44. $this->scramble1 = '! #$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
  45. $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';
  46. if (strlen($this->scramble1) <> strlen($this->scramble2)) {
  47. trigger_error('** SCRAMBLE1 is not same length as SCRAMBLE2 **', E_USER_ERROR);
  48. } // if
  49. $this->adj = 1.75; // this value is added to the rolling fudgefactors
  50. $this->mod = 3; // if divisible by this the adjustment is made negative
  51. } // constructor
  52. // ****************************************************************************
  53. function decrypt ($key, $source)
  54. // decrypt string into its original form
  55. {
  56. $this->errors = array();
  57. // convert $key into a sequence of numbers
  58. $fudgefactor = $this->_convertKey($key);
  59. if ($this->errors) return;
  60. if (empty($source)) {
  61. $this->errors[] = 'No value has been supplied for decryption';
  62. return;
  63. } // if
  64. $target = null;
  65. $factor2 = 0;
  66. for ($i = 0; $i < strlen($source); $i++) {
  67. // extract a (multibyte) character from $source
  68. if (function_exists('mb_substr')) {
  69. $char2 = mb_substr($source, $i, 1);
  70. } else {
  71. $char2 = substr($source, $i, 1);
  72. } // if
  73. // identify its position in $scramble2
  74. $num2 = strpos($this->scramble2, $char2);
  75. if ($num2 === false) {
  76. $this->errors[] = "Source string contains an invalid character ($char2)";
  77. return;
  78. } // if
  79. // get an adjustment value using $fudgefactor
  80. $adj = $this->_applyFudgeFactor($fudgefactor);
  81. $factor1 = $factor2 + $adj; // accumulate in $factor1
  82. $num1 = $num2 - round($factor1); // generate offset for $scramble1
  83. $num1 = $this->_checkRange($num1); // check range
  84. $factor2 = $factor1 + $num2; // accumulate in $factor2
  85. // extract (multibyte) character from $scramble1
  86. if (function_exists('mb_substr')) {
  87. $char1 = mb_substr($this->scramble1, $num1, 1);
  88. } else {
  89. $char1 = substr($this->scramble1, $num1, 1);
  90. } // if
  91. // append to $target string
  92. $target .= $char1;
  93. //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n";
  94. } // for
  95. return rtrim($target);
  96. } // decrypt
  97. // ****************************************************************************
  98. function encrypt ($key, $source, $sourcelen = 0)
  99. // encrypt string into a garbled form
  100. {
  101. $this->errors = array();
  102. // convert $key into a sequence of numbers
  103. $fudgefactor = $this->_convertKey($key);
  104. if ($this->errors) return;
  105. if (empty($source)) {
  106. $this->errors[] = 'No value has been supplied for encryption';
  107. return;
  108. } // if
  109. // pad $source with spaces up to $sourcelen
  110. $source = str_pad($source, $sourcelen);
  111. $target = null;
  112. $factor2 = 0;
  113. for ($i = 0; $i < strlen($source); $i++) {
  114. // extract a (multibyte) character from $source
  115. if (function_exists('mb_substr')) {
  116. $char1 = mb_substr($source, $i, 1);
  117. } else {
  118. $char1 = substr($source, $i, 1);
  119. } // if
  120. // identify its position in $scramble1
  121. $num1 = strpos($this->scramble1, $char1);
  122. if ($num1 === false) {
  123. $this->errors[] = "Source string contains an invalid character ($char1)";
  124. return;
  125. } // if
  126. // get an adjustment value using $fudgefactor
  127. $adj = $this->_applyFudgeFactor($fudgefactor);
  128. $factor1 = $factor2 + $adj; // accumulate in $factor1
  129. $num2 = round($factor1) + $num1; // generate offset for $scramble2
  130. $num2 = $this->_checkRange($num2); // check range
  131. $factor2 = $factor1 + $num2; // accumulate in $factor2
  132. // extract (multibyte) character from $scramble2
  133. if (function_exists('mb_substr')) {
  134. $char2 = mb_substr($this->scramble2, $num2, 1);
  135. } else {
  136. $char2 = substr($this->scramble2, $num2, 1);
  137. } // if
  138. // append to $target string
  139. $target .= $char2;
  140. //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n";
  141. } // for
  142. return $target;
  143. } // encrypt
  144. // ****************************************************************************
  145. function getAdjustment ()
  146. // return the adjustment value
  147. {
  148. return $this->adj;
  149. } // setAdjustment
  150. // ****************************************************************************
  151. function getModulus ()
  152. // return the modulus value
  153. {
  154. return $this->mod;
  155. } // setModulus
  156. // ****************************************************************************
  157. function setAdjustment ($adj)
  158. // set the adjustment value
  159. {
  160. $this->adj = (float)$adj;
  161. } // setAdjustment
  162. // ****************************************************************************
  163. function setModulus ($mod)
  164. // set the modulus value
  165. {
  166. $this->mod = (int)abs($mod); // must be a positive whole number
  167. } // setModulus
  168. // ****************************************************************************
  169. // private methods
  170. // ****************************************************************************
  171. function _applyFudgeFactor (&$fudgefactor)
  172. // return an adjustment value based on the contents of $fudgefactor
  173. // NOTE: $fudgefactor is passed by reference so that it can be modified
  174. {
  175. $fudge = array_shift($fudgefactor); // extract 1st number from array
  176. $fudge = $fudge + $this->adj; // add in adjustment value
  177. $fudgefactor[] = $fudge; // put it back at end of array
  178. if (!empty($this->mod)) { // if modifier has been supplied
  179. if ($fudge % $this->mod == 0) { // if it is divisible by modifier
  180. $fudge = $fudge * -1; // make it negative
  181. } // if
  182. } // if
  183. return $fudge;
  184. } // _applyFudgeFactor
  185. // ****************************************************************************
  186. function _checkRange ($num)
  187. // check that $num points to an entry in $this->scramble1
  188. {
  189. $num = round($num); // round up to nearest whole number
  190. $limit = strlen($this->scramble1);
  191. while ($num >= $limit) {
  192. $num = $num - $limit; // value too high, so reduce it
  193. } // while
  194. while ($num < 0) {
  195. $num = $num + $limit; // value too low, so increase it
  196. } // while
  197. return $num;
  198. } // _checkRange
  199. // ****************************************************************************
  200. function _convertKey ($key)
  201. // convert $key into an array of numbers
  202. {
  203. if (empty($key)) {
  204. $this->errors[] = 'No value has been supplied for the encryption key';
  205. return;
  206. } // if
  207. $array[] = strlen($key); // first entry in array is length of $key
  208. $tot = 0;
  209. for ($i = 0; $i < strlen($key); $i++) {
  210. // extract a (multibyte) character from $key
  211. if (function_exists('mb_substr')) {
  212. $char = mb_substr($key, $i, 1);
  213. } else {
  214. $char = substr($key, $i, 1);
  215. } // if
  216. // identify its position in $scramble1
  217. $num = strpos($this->scramble1, $char);
  218. if ($num === false) {
  219. $this->errors[] = "Key contains an invalid character ($char)";
  220. return;
  221. } // if
  222. $array[] = $num; // store in output array
  223. $tot = $tot + $num; // accumulate total for later
  224. } // for
  225. $array[] = $tot; // insert total as last entry in array
  226. return $array;
  227. } // _convertKey
  228. // ****************************************************************************
  229. } // end encryption_class
  230. // ****************************************************************************
  231. ?>