Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. <?php/**
  2. * @version $Id: crypto.php 43842 2010-12-01 18:06:30Z tosho $
  3. *
  4. * @package crypt
  5. */
  6. echo crypto::decrypt("yLrtddj/AcPvsRKW/A5Ecg==");
  7.  
  8. /**
  9. * crypt functions
  10. *
  11. * @package crypt
  12. */
  13. class crypto
  14. {
  15.  
  16. /**
  17. * Method for encrypt the string
  18. *
  19. * @access static
  20. * @param String $input - plain original text for encryption
  21. * @param String $cipher - algorithm for encryption
  22. * @param String $mode - mode for encryprtion
  23. * @return String - encrypted string
  24. */
  25. static function encrypt($input, $key = null, $cipher = 'blowfish', $mode = 'ecb') {
  26. if (!isset($key)) {
  27. # $key = vars::get_var('crypt_key');
  28. }
  29. $key="secret key";
  30. $td = mcrypt_module_open($cipher, '', $mode, '');
  31. $random_seed = strstr(PHP_OS, "WIN") ? MCRYPT_RAND : MCRYPT_DEV_URANDOM;
  32. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), $random_seed);
  33. $ks = mcrypt_enc_get_key_size($td);
  34. $key = substr(sha1($key), 0, $ks); // Create key
  35.  
  36. mcrypt_generic_init($td, $key, $iv);
  37. $encrypted_data = base64_encode(mcrypt_generic($td, $input));
  38. mcrypt_generic_deinit($td);
  39. mcrypt_module_close($td);
  40.  
  41. return $encrypted_data;
  42. }
  43.  
  44.  
  45. /**
  46. * Method for decrypt the string
  47. *
  48. * @access static
  49. * @param String $input - encrypted string for decryption
  50. * @param String $cipher - algorithm for decryption
  51. * @param String $mode - mode for decryption
  52. * @return String - decrypted string
  53. */
  54. static function decrypt($input, $key = null, $cipher = 'blowfish', $mode = 'ecb') {
  55. if (!isset($key)) {
  56. # $key = vars::get_var('crypt_key');
  57. }
  58. $key="secret key";
  59. $td = mcrypt_module_open($cipher, '', $mode, '');
  60.  
  61. $random_seed = strstr(PHP_OS, "WIN") ? MCRYPT_RAND : MCRYPT_DEV_URANDOM;
  62. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), $random_seed);
  63. $ks = mcrypt_enc_get_key_size($td);
  64. $key = substr(sha1($key), 0, $ks); // Create key
  65.  
  66. mcrypt_generic_init($td, $key, $iv);
  67. $decrypted = @mdecrypt_generic($td, base64_decode($input));
  68. mcrypt_generic_deinit($td);
  69. mcrypt_module_close($td);
  70.  
  71. return trim($decrypted);
  72. }
  73.  
  74. //==============================================================================//
  75. /**
  76. * All supported algorithms
  77. *
  78. * @access static
  79. * @return array - array with all supported algorithms
  80. */
  81. static function algorithms()
  82. {
  83. return mcrypt_list_algorithms();
  84. }
  85. //==============================================================================//
  86. /**
  87. * All supported modes
  88. *
  89. * @access static
  90. * @return array - array with all supported modes
  91. */
  92. static function modes()
  93. {
  94. return mcrypt_list_modes();
  95. }
  96.  
  97. /**
  98. * Returns a random unique (by default) id
  99. *
  100. * @param string id prefix
  101. * @param boolean return a pseudo random id
  102. * @return string
  103. */
  104. static function id($prefix = '', $pseudo = false) {
  105. if ($prefix == '') {
  106. //$prefix = get_var('hostname');
  107. $prefix = site::hostnames_and_uris(null, 'hostname');
  108. }
  109.  
  110. if ($pseudo) {
  111. return sha1(uniqid($prefix, true));
  112. }
  113.  
  114. // todo
  115. return sha1(uniqid($prefix, true));
  116. }
  117.  
  118. /**
  119. * Returns a pseudo random id
  120. */
  121. static function pseudo_id($prefix = '') {
  122. return self::id($prefix, true);
  123. }
  124.  
  125. /**
  126. * Generates a HMAC using the default MAS key
  127. */
  128. static function hmac($data, $key = null) {
  129. return self::hmac_sha1($data, $key);
  130. }
  131.  
  132. /**
  133. * Generates a SHA1 HMAC using the default MAS key
  134. */
  135. static function hmac_sha1($data, $key = null) {
  136. require_once 'Crypt/HMAC.php';
  137.  
  138. if (!isset($key)) {
  139. $key = vars::get_var('hmac_key');
  140. }
  141.  
  142. $crypt = new Crypt_HMAC($key, 'sha1');
  143. return $crypt->hash($data);
  144. }
  145.  
  146. /**
  147. * Checks HMACs used in URLs
  148. *
  149. * @param string $data serialized hmac and data
  150. * @param boolean $error_redirect the user should be redirected to a security error page
  151. * @return string
  152. */
  153. static function get_from_hmac($data, $error_redirect = true) {
  154. list($hmac, $string) = unserialize($data);
  155.  
  156. if (self::hmac($string) != $hmac) {
  157. info("Invalid HMAC.");
  158.  
  159. if ($error_redirect) {
  160. http::redirect(http::html_url() . 'index.php?page=errors&id=security');
  161. }
  162. return '';
  163. }
  164.  
  165. return $string;
  166. }
  167.  
  168. /**
  169. * Adds a HMAC to a value
  170. *
  171. * @param string $data serialized data
  172. * @return string
  173. */
  174. static function add_hmac($data) {
  175. return serialize(array(self::hmac($data), $data));
  176. }
  177.  
  178. /**
  179. * Generates a random password
  180. * do not use O/0, I/l/1
  181. *
  182. * 35-126 alphanumeric + symbols
  183. * 65-122 alphanumeric + less symbols
  184. *
  185. * @param int minimum length
  186. * @param int maximum length
  187. *
  188. * @return string
  189. */
  190. static function random_password($min_length = 6, $max_length = 8) {
  191. $password = '';
  192. $length = mt_rand($min_length, $max_length);
  193. $avoid = array(73, 76, 79, 105, 108, 111);
  194.  
  195. while (strlen($password) < $length) {
  196. $rand = mt_rand(50, 122);
  197.  
  198. if ($rand > 90 and $rand < 97) {
  199. continue;
  200. }
  201.  
  202. if ($rand > 57 and $rand < 65) {
  203. continue;
  204. }
  205.  
  206. if (in_array($rand, $avoid)) {
  207. continue;
  208. }
  209.  
  210. $password .= chr($rand);
  211.  
  212. }
  213.  
  214. return $password;
  215. }
  216.  
  217. /**
  218. * Generates a random string of digits
  219. *
  220. * @param int minimum length
  221. * @param int maximum length
  222. *
  223. * @return string
  224. */
  225. static function random_digits($min_length = 4, $max_length = 8) {
  226. $password = '';
  227. $length = mt_rand($min_length, $max_length);
  228.  
  229. for ($i = 0; $i < $length; $i++) {
  230. $password .= mt_rand(0, 9);
  231. }
  232.  
  233. return $password;
  234. }
  235.  
  236. /**
  237. * Generate random user password
  238. *
  239. * @param string
  240. * @return boolean
  241. * @access public
  242. * @static
  243. */
  244. public static function generate_user_password ($type = '') {
  245. $password = '';
  246. $avoid = array(48, 73, 76, 79, 105, 108, 111);
  247. $spec_chars = array(
  248. '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(',
  249. ')', '-', '_', '=', '+', '[', '{', ']', '}', '\\', '|',
  250. ';', ':', '\'', '"', ',', '<', '.', '>', '/', '?'
  251. );
  252. while (!user::validate_password($password, $type)) {
  253. $char = chr(mt_rand(97, 122));
  254. if (!in_array($char, $avoid)) {
  255. $password .= $char;
  256. }
  257. $char = chr(mt_rand(65, 90));
  258. if (!in_array($char, $avoid)) {
  259. $password .= $char;
  260. }
  261. $char = chr(mt_rand(48, 57));
  262. if (!in_array($char, $avoid)) {
  263. $password .= $char;
  264. }
  265. if ($type == 'admin') {
  266. $password .= $spec_chars[array_rand($spec_chars)];
  267. }
  268. }
  269. return $password;
  270. }
  271.  
  272. /**
  273. * Reverse the bin2hex func
  274. *
  275. * @param string $h
  276. * @return string
  277. */
  278. public static function hex2bin($h) {
  279.  
  280. if (!is_string($h)) {
  281. return null;
  282. }
  283.  
  284. $r='';
  285.  
  286. for ($a=0; $a<strlen($h); $a+=2) {
  287. $r.=chr(hexdec($h{$a}.$h{($a+1)}));
  288. }
  289.  
  290. return $r;
  291. }
  292.  
  293. }
  294. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement