Php source code analysis-DZX1.5 encryption and decryption function authcode usage

Source: Internet
Author: User
This article mainly introduces the usage of DZX1.5 encryption and decryption function authcode in php source code analysis. The example shows how to use the authcode function in DZX1.5 for encryption and decryption. For more information, see

This article mainly introduces the usage of DZX1.5 encryption and decryption function authcode in php source code analysis. The example shows how to use the authcode function in DZX1.5 for encryption and decryption. For more information, see

This article describes how to use the authcode function of DZX1.5 encryption and decryption in php source code analysis. Share it with you for your reference. The specific analysis is as follows:

<? Php $ authkey = '';/*** @ param string $ string: Enter the plaintext (or ciphertext) to be encrypted (or decrypted) * @ param string $ operation: 'decode' or others, where decryption is performed by default. Input other values to encrypt * @ param string $ key: encryption/decryption key * @ param int $ expiry: validity Period */function authcode ($ string, $ operation = 'decode', $ key = '', $ expiry = 0) {global $ authkey; $ ckey_length = 4; $ key = md5 ($ key! = ''? $ Key: $ authkey); $ keya = md5 (substr ($ key, 0, 16); $ keyb = md5 (substr ($ 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) ;}}$ str = '000000'; $ key = '000000'; echo" plaintext :". $ str; echo"
"; Echo" key: ". $ key; $ encode = authcode ($ str, '11', $ key); echo"
"; Echo" encrypted ciphertext: ". $ encode; echo"
"; $ Decode = authcode ($ encode, 'decode', $ key); echo" decrypted plaintext: ". $ DECODE;/* End of php */

The running result is as follows:

Plaintext: 1234
Secret Key: 1234
Encrypted ciphertext: a52f67eXZGVy0HtQBo4vOREhq3WxnL6E2zlx75JGfoJW
Decrypted plaintext: 1234

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.