Php source code analysis-DZX1.5 encryption and decryption function authcode usage, dzx1.5authcode
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 "<br/>"; echo "key :". $ key; $ encode = authcode ($ str, '11', $ key); echo "<br/>"; echo "encrypted ciphertext :". $ encode; echo "<br/>"; $ 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.