PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/simplecrypt.class.inc.php

https://github.com/adiakin/itop
PHP | 235 lines | 126 code | 19 blank | 90 comment | 12 complexity | a0cc3be5b01aede1088b09568adbfb71 MD5 | raw file
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * SimpleCrypt Class - crypto helpers
  18. * Simple encryption of strings, uses mcrypt or degrades to a pure PHP
  19. * implementation when mcrypt is not present.
  20. * Based on Miguel Ros' work found at:
  21. * http://rossoft.wordpress.com/2006/05/22/simple-encryption-class/
  22. *
  23. * Usage:
  24. * $oSimpleCrypt = new SimpleCrypt();
  25. * $encrypted = $oSimpleCrypt->encrypt('a_key','the_text');
  26. * $sClearText = $oSimpleCrypt->decrypt('a_key',$encrypted);
  27. *
  28. * The result is $plain equals to 'the_text'
  29. *
  30. * You can use a different engine if you don't have Mcrypt:
  31. * $oSimpleCrypt = new SimpleCrypt('Simple');
  32. *
  33. * A string encrypted with one engine can't be decrypted with
  34. * a different one even if the key is the same.
  35. *
  36. * @author Miguel Ros <rossoft@gmail.com>
  37. * @author Erwan Taloc <erwan.taloc@combodo.com>
  38. * @author Romain Quetiez <romain.quetiez@combodo.com>
  39. * @author Denis Flaven <denis.flaven@combodo.com>
  40. * @version 0.3
  41. * @license GPL
  42. */
  43. class SimpleCrypt
  44. {
  45. /**
  46. * Constructor
  47. * @param string $sEngineName Engine for encryption. Values: Simple, Mcrypt
  48. */
  49. function __construct($sEngineName = 'Mcrypt')
  50. {
  51. if (($sEngineName == 'Mcrypt') && (!function_exists('mcrypt_module_open')))
  52. {
  53. // Defaults to Simple encryption if the mcrypt module is not present
  54. $sEngineName = 'Simple';
  55. }
  56. $sEngineName = 'SimpleCrypt' . $sEngineName . 'Engine';
  57. $this->oEngine = new $sEngineName;
  58. }
  59. /**
  60. * Encrypts the string with the given key
  61. * @param string $key
  62. * @param string $sString Plaintext string
  63. * @return string Ciphered string
  64. */
  65. function Encrypt($key, $sString)
  66. {
  67. return $this->oEngine->Encrypt($key,$sString);
  68. }
  69. /**
  70. * Decrypts the string by the given key
  71. * @param string $key
  72. * @param string $string Ciphered string
  73. * @return string Plaintext string
  74. */
  75. function Decrypt($key, $string)
  76. {
  77. return $this->oEngine->Decrypt($key,$string);
  78. }
  79. /**
  80. * Returns a random "salt" value, to be used when "hashing" a password
  81. * using a one-way encryption algorithm, to prevent an attack using a "rainbow table"
  82. * Tryes to use the best available random number generator
  83. * @return string The generated random "salt"
  84. */
  85. static function GetNewSalt()
  86. {
  87. // Copied from http://www.php.net/manual/en/function.mt-rand.php#83655
  88. // get 128 pseudorandom bits in a string of 16 bytes
  89. $sRandomBits = null;
  90. // Unix/Linux platform?
  91. $fp = @fopen('/dev/urandom','rb');
  92. if ($fp !== FALSE)
  93. {
  94. //echo "Random bits pulled from /dev/urandom<br/>\n";
  95. $sRandomBits .= @fread($fp,16);
  96. @fclose($fp);
  97. }
  98. else
  99. {
  100. // MS-Windows platform?
  101. if (@class_exists('COM'))
  102. {
  103. // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx
  104. try
  105. {
  106. $CAPI_Util = new COM('CAPICOM.Utilities.1');
  107. $sBase64RandomBits = ''.$CAPI_Util->GetRandom(16,0);
  108. // if we ask for binary data PHP munges it, so we
  109. // request base64 return value. We squeeze out the
  110. // redundancy and useless ==CRLF by hashing...
  111. if ($sBase64RandomBits)
  112. {
  113. //echo "Random bits got from CAPICOM.Utilities.1<br/>\n";
  114. $sRandomBits = md5($sBase64RandomBits, TRUE);
  115. }
  116. }
  117. catch (Exception $ex)
  118. {
  119. // echo 'Exception: ' . $ex->getMessage();
  120. }
  121. }
  122. }
  123. if ($sRandomBits == null)
  124. {
  125. // No "strong" random generator available, use PHP's built-in mechanism
  126. //echo "Random bits generated from mt_rand<br/>\n";
  127. mt_srand(crc32(microtime()));
  128. $sRandomBits = '';
  129. for($i = 0; $i < 4; $i++)
  130. {
  131. $sRandomBits .= sprintf('%04x', mt_rand(0, 65535));
  132. }
  133. }
  134. return $sRandomBits;
  135. }
  136. }
  137. /**
  138. * Interface for encryption engines
  139. */
  140. interface CryptEngine
  141. {
  142. function Encrypt($key, $sString);
  143. function Decrypt($key, $encrypted_data);
  144. }
  145. /**
  146. * Simple Engine doesn't need any PHP extension.
  147. * Every encryption of the same string with the same key
  148. * will return the same encrypted string
  149. */
  150. class SimpleCryptSimpleEngine implements CryptEngine
  151. {
  152. public function Encrypt($key, $sString)
  153. {
  154. $result = '';
  155. for($i=1; $i<=strlen($sString); $i++)
  156. {
  157. $char = substr($sString, $i-1, 1);
  158. $keychar = substr($key, ($i % strlen($key))-1, 1);
  159. $char = chr(ord($char)+ord($keychar));
  160. $result.=$char;
  161. }
  162. return $result;
  163. }
  164. public function Decrypt($key, $encrypted_data)
  165. {
  166. $result = '';
  167. for($i=1; $i<=strlen($encrypted_data); $i++)
  168. {
  169. $char = substr($encrypted_data, $i-1, 1);
  170. $keychar = substr($key, ($i % strlen($key))-1, 1);
  171. $char = chr(ord($char)-ord($keychar));
  172. $result.=$char;
  173. }
  174. return $result;
  175. }
  176. }
  177. /**
  178. * McryptEngine requires Mcrypt extension
  179. * Every encryption of the same string with the same key
  180. * will return a different encrypted string.
  181. */
  182. class SimpleCryptMcryptEngine implements CryptEngine
  183. {
  184. var $alg = MCRYPT_BLOWFISH;
  185. var $td = null;
  186. public function __construct()
  187. {
  188. $this->td = mcrypt_module_open($this->alg,'','cbc','');
  189. }
  190. public function Encrypt($key, $sString)
  191. {
  192. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND); // MCRYPT_RAND is the only choice on Windows prior to PHP 5.3
  193. mcrypt_generic_init($this->td, $key, $iv);
  194. if (empty($sString))
  195. {
  196. $sString = str_repeat("\0", 8);
  197. }
  198. $encrypted_data = mcrypt_generic($this->td, $sString);
  199. mcrypt_generic_deinit($this->td);
  200. return $iv.$encrypted_data;
  201. }
  202. public function Decrypt($key, $encrypted_data)
  203. {
  204. $iv = substr($encrypted_data, 0, mcrypt_enc_get_iv_size($this->td));
  205. $string = substr($encrypted_data, mcrypt_enc_get_iv_size($this->td));
  206. mcrypt_generic_init($this->td, $key, $iv);
  207. $decrypted_data = rtrim(mdecrypt_generic($this->td, $string), "\0");
  208. mcrypt_generic_deinit($this->td);
  209. return $decrypted_data;
  210. }
  211. public function __destruct()
  212. {
  213. mcrypt_module_close($this->td);
  214. }
  215. }
  216. ?>