Discuz encryption and decryption function using method and Chinese annotation

Source: Internet
Author: User
Tags md5 ord strlen

  This article mainly introduces the use of Discuz encryption and decryption function and Chinese annotation, we refer to the use of the bar

  Code as follows: <?php/**  * $string plaintext or ciphertext  * $operation encrypt encode or decrypt decode  * $key key  * $expiry key validity  */  function Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0} {   //dynamic key length, same plaintext The generation of different ciphertext is relying on dynamic key    //join with the secret key, can make the cipher without any regularity, even if the original and key is identical, the encryption results will be different each time, increase the difficulty of cracking.    //value, the greater the ciphertext change law, ciphertext change = 16 $ckey _length    //When this value is 0 o'clock, does not produce with the secret key     $ckey _length = 4;      //Key    //$GLOBALS [' Discuz_auth_key '] Here you can modify the     $key = MD5 ($key? $key : $GLOBALS [' Discuz_auth_key ']);       //Key A will participate in encryption and decryption     $keya = MD5 (substr ($key, 0, 16));    //key B will be used to do data integrity validation     $KEYB = MD5 (substr ($key, 16, 16));    //key C for change-generated redaction     $KEYC = $ckey _length? ($operation = = ' DECODE ' substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): ";    //participation key     $cryptkey = $keya. MD5 ($keya. $KEYC);     $key _length = strlen ($cryptkey);    //Clear text, the first 10 bits used to save the timestamp, verify the validity of the data when decrypting, 10 to 26 bits to save $keyb (key B), decryption will verify data integrity through this key    //If it is decoded, it will be from the first $ckey_ Length bit start, because the ciphertext before $ckey_length bit to save the dynamic key to ensure decryption correct     $string = $operation = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)): sprintf ('%010d ', $expiry? $expiry + Time (): 0). substr (MD5 ($string. $keyb , 0). $string;     $string _length = strlen ($string);     $result = ';     $box = range (0, 255);     $rndkey = array ();    //generating key book     for ($i = 0; $i <= 255 $i + +) {        $rndkey [$i] = Ord ($cryp tkey[$i% $key _length]);    }    //With fixed algorithm, scrambling key book, increase randomness, it seems very complicated, actually will not increase the intensity of the ciphertext     for ($j = $i = 0; $i < 256; $i + +) {        $J = ($j + $box [$i] + $rndkey [$i])% 256         $tmp = $box [$i]; &nbsp ;       $box [$i] = $box [$j];        $box [$j] = $tmp;    }    //Core plus decryption section     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;        //from the key book to the key to the different or to the character         $result. = Chr (ord ($string [$i]) ^ ($box [($bo x[$a] + $box [$j])% 256]);    }     if ($operation = = ' DECODE ') {       //substr ($result, 0, 10) = 0 Validating data Validity        //substr ($result, 0,)-time () > 0 Validation data validation        //substr ($r Esult = = substr (MD5 (substr ($result,). $keyb), 0, 16) Verify data integrity        //Validate data validation, see unencrypted plaintext format         if (substr ($result, 0,) = 0 | | substr ($result, 0, 10)-time () > 0) && substr ($result) = = substr (MD5 (substr ($result,). $keyb), 0) {    &nbs P       return substr ($result, 26);        } else {            return ';     '    } &N Bsp  } else {       //Keep the dynamic key in the ciphertext, which is why the same plaintext, production of different ciphertext can be decrypted after the reason of the        //Because of the encrypted Redaction may be some special characters, the copy process may be lost, so use base64 encoding         return $KEYC. Str_replace (' = ', ', Base64_encode ($result) );    }     $a = www.jb51.net; $b = Authcode ($a, "ENCODE", "abc123"); echo $b. " <br/> "; Echo Authcode ($b, "DECODE", "abc123");?>  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.