Share a PHP encryption/decryption class

Source: Internet
Author: User
Share a PHP encryption/decryption class

  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 to be encrypted
  16. * @ Param String $ skey key
  17. * @ Param int $ expiry ciphertext validity period. Valid during encryption. unit: Seconds. 0 indicates permanent validity.
  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 to be decrypted
  39. * @ Param String $ skey 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. }


Encryption and decryption, PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.