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.