PHP encryption and decryption instance analysis and encryption and decryption instance analysis

Source: Internet
Author: User

PHP encryption and decryption instance analysis and encryption and decryption instance analysis

This example describes the PHP encryption and decryption class. Share it with you for your reference. The specific analysis is as follows:

This Code supports array encryption, ciphertext validity period, and various symmetric encryption

The parameters are as follows:

* @ Use ption: en ($ string, $ key );
* @ Param String $ string the String to be encrypted
* @ Param String $ skey key
* @ Param int $ expiry ciphertext validity period. Valid during encryption. Unit: seconds. 0 indicates permanent validity.
* @ Return String

1. the php code is as follows:

/**-Tool library-password encryption/Decryption */class ption {private static $ original = array ('=', '+ ','/'); private static $ later = array ('oss 0o', 'oss 0o', 'oo00o'); function _ construct () {} private static function md5 ($ skey = '') {$ skey = $ skey? $ Skey: 'ui'; // uicms: _ config ('security/authkey'); return md5 (substr ($ skey, 0, 16 ));} /*** @ use ption: en ($ string, $ key ); * @ param String $ string the String to be encrypted * @ param String $ skey key * @ param int $ expiry ciphertext validity period, valid during encryption, in seconds, 0 is permanently valid * @ return String */static public function en ($ string = '', $ skey ='', $ expiry = 0) {if (is_array ($ string) {$ string = json_encode ($ string); // uicms: json ($ string, True, 'en');} $ string = str_pad ($ expiry? $ Expiry + TIME: 0, 10, 0 ). $ string; $ strArr = str_split (base64_encode ($ string); $ strCount = count ($ strArr); $ skey = static: md5 ($ skey ); foreach (str_split ($ skey) as $ key = >$ value) {$ key <$ strCount & $ strArr [$ key]. = $ value;} return str_replace (self ::$ original, self ::$ later, join ('', $ strArr);}/*** @ use ption :: de ($ string, $ key ); * @ param String $ string the String to be decrypted * @ param String $ skey key * @ return string */static public function de ($ String = '', $ skey = '') {$ strArr = str_split (str_replace (self ::$ later, self ::$ original, $ string), 2); $ strCount = count ($ strArr ); $ skey = static: md5 ($ skey); foreach (str_split ($ skey) as $ key => $ value) {$ key <$ strCount & $ strArr [$ key] [1] ===$ value & $ strArr [$ key] = $ strArr [$ key] [0] ;} $ result = base64_decode (join ('', $ strArr); if (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10) -TIME> 0) {return substr ($ result, 10) ;}else {return false ;}}}

2. Usage:

$str['username'] = 'oschina';$str['pw'] = '123456';$str['huoxin'] = '!@#$%^&';echo "string : " . $str . " <br />";echo "encode : " . ($enstring = ption::en($str)) . '<br />';echo "decode : " . ption::de($enstring);

I hope this article will help you with php programming.

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.