Share a PHP encryption and decryption class

WBOY
Release: 2016-07-25 08:44:12
Original
790 people have browsed it
  1. class ption
  2. {
  3. private static $original = array('=', '+', '/');
  4. private static $later = array('O0O0O', 'o0O0o', 'oo00o');
  5. function __construct()
  6. {
  7. }
  8. private static function md5($skey = '')
  9. {
  10. $skey = $skey ? $skey : 'ui' ; //uicms::_config('security/authkey');
  11. return md5(substr($skey, 0, 16));
  12. }
  13. /**
  14. * @use ption::en($string, $key);
  15. * @param String $string The string that needs to be encrypted
  16. * @param String $skey The key
  17. * @param int $expiry The validity period of the ciphertext, when encrypted Valid, unit is second, 0 is permanently valid
  18. * @return String
  19. */
  20. static public function en($string = '', $skey = '', $expiry=0)
  21. {
  22. if( is_array( $string ) )
  23. {
  24. $string = json_encode($string); // uicms::json($string, true, 'en');
  25. }
  26. $string = str_pad($expiry ? $expiry + TIME : 0, 10, 0).$string;
  27. $strArr = str_split(base64_encode($string));
  28. $strCount = count($strArr);
  29. $skey = static::md5($skey);
  30. foreach (str_split($skey) as $key => $value)
  31. {
  32. $key < $strCount && $strArr[$key].=$value;
  33. }
  34. return str_replace(self::$original, self::$later, join('', $strArr));
  35. }
  36. /**
  37. * @use ption::de($string, $key);
  38. * @param String $string The string that needs to be decrypted
  39. * @param String $skey The key
  40. * @return String
  41. */
  42. static public function de($string = '', $skey = '')
  43. {
  44. $strArr = str_split(str_replace(self::$later, self::$original, $string), 2);
  45. $strCount = count($strArr);
  46. $skey = static::md5($skey);
  47. foreach (str_split($skey) as $key => $value)
  48. {
  49. $key <= $strCount && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
  50. }
  51. $result = base64_decode(join('', $strArr));
  52. if(substr($result, 0, 10) == 0 || substr($result, 0, 10) - TIME > 0)
  53. {
  54. return substr($result, 10);
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. }
  61. }
复制代码

加密解密, PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template