PHP encryption and decryption instance code, php encryption and decryption instance

Source: Internet
Author: User

PHP encryption and decryption instance code, php encryption and decryption instance

The key code is as follows:

<? Phpfunction I _array_column ($ input, $ columnKey, $ indexKey = null) {if (! Function_exists ('array _ column ') {$ columnKeyIsNumber = (is_numeric ($ columnKey ))? True: false; $ indexKeyIsNull = (is_null ($ indexKey ))? True: false; $ indexKeyIsNumber = (is_numeric ($ indexKey ))? True: false; $ result = array (); foreach (array) $ input as $ key => $ row) {if ($ columnKeyIsNumber) {$ tmp = array_slice ($ row, $ columnKey, 1); $ tmp = (is_array ($ tmp )&&! Empty ($ tmp ))? Current ($ tmp): null;} else {$ tmp = isset ($ row [$ columnKey])? $ Row [$ columnKey]: null;} if (! $ IndexKeyIsNull) {if ($ indexKeyIsNumber) {$ key = array_slice ($ row, $ indexKey, 1); $ key = (is_array ($ key )&&! Empty ($ key ))? Current ($ key): null; $ key = is_null ($ key )? 0: $ key;} else {$ key = isset ($ row [$ indexKey])? $ Row [$ indexKey]: 0 ;}$ result [$ key] = $ tmp;} return $ result;} else {return array_column ($ input, $ columnKey, $ indexKey) ;}} function randcode ($ len, $ mode = 2) {$ rcode = ''; switch ($ mode) {case 1: // remove confusing characters such as 0, o, O, and l. $ chars = 'taobao'; break; case 2: // pure number $ chars = '2016'; break; case 3: // All numbers + uppercase/lowercase letters $ chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrs Tuvwxyz '; break; case 4: // full number + uppercase/lowercase letters + special characters $ chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ~! @ # $ % ^ & * () '; Break;} $ count = strlen ($ chars)-1; mt_srand (double) microtime () * 1000000 ); for ($ I = 0; $ I <$ len; $ I ++) {$ rcode. = $ chars [mt_rand (0, $ count)];} return $ rcode ;} /*** $ string plaintext or ciphertext * $ operation encryption ENCODE or decryption DECODE * $ key * $ expiry key validity period */function authcode ($ string, $ operation = 'decode', $ key = '', $ expiry = 0) {$ ckey_length = 4; $ key = md5 ($ key ); $ keya = md5 (substr ($ key, 0, 16); $ keyb = md5 (subs Tr ($ key, 16, 16); $ keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length): substr (md5 (microtime (),-$ ckey_length): ''; $ cryptkey = $ keya. md5 ($ keya. $ keyc); $ key_length = strlen ($ cryptkey); $ string = $ operation = 'decode '? Base64_decode (substr ($ string, $ ckey_length): sprintf ('% 010d', $ expiry? $ Expiry + time (): 0 ). substr (md5 ($ string. $ keyb), 0, 16 ). $ string; $ string_length = strlen ($ string); $ result = ''; $ box = range (0,255); $ rndkey = array (); for ($ I = 0; $ I <= 255; $ I ++) {$ rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);} for ($ j = $ I = 0; I I <256; $ I ++) {$ j = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256; $ tmp = $ box [$ I]; $ box [$ I] = $ box [$ j]; $ box [$ j] = $ tmp;} for ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++) {$ a = ($ a + 1) % 256; $ j = ($ j + $ box [$ a]) % 256; $ tmp = $ box [$ a]; $ box [$ a] = $ box [$ j]; $ box [$ j] = $ tmp; $ result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);} if ($ operation = 'decode') {if (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10) -time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16) {return substr ($ result, 26) ;}else {return '';}} else {return $ keyc. str_replace ('=', '', base64_encode ($ result ));}}

The above is the PHP encryption and decryption instance Code introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.