PHP Reversible encryption algorithm

Source: Internet
Author: User

Code:
1<?PHP2 3 classTest {4 5     Private $authCodeKey= ' KHUVFB9PIJNYCYMGZDZQEKALYG7DH '; 6 7     functionAuthcode ($input,$key) {8 9         #Input must be of even length.Ten         if(strlen($input)% 2) { One             //$input. = ' 0 '; A         } -  -         #The Keys longer than the input would be truncated. the         if(strlen($key) >strlen($input)) { -             $key=substr($key, 0,strlen($input)); -         } -  +         #The Keys shorter than the input would be padded. -         if(strlen($key) <strlen($input)) { +             $key=Str_pad($key,strlen($input), ' 0 ',str_pad_right); A         } at  -         #Now the key and input is the same length. -         #Zero is used to any trailing padding required. -  -         #Simple XOR ' ing, each input byte with each key byte. -         $result= ' '; in          for($i= 0;$i<strlen($input);$i++) { -             $result.=$input{$i} ^$key{$i}; to         } +         return $result; -     } the  *     /** $ * EncryptionPanax Notoginseng      */ -     functionEncrypt$sessionId) { the  +         $hashKey=$this->base64url_encode ($this->authcode ($sessionId,$this-authcodekey)); A         $hashKey=$this->base64url_encode ($sessionId); the         return $hashKey; +     } -  $  $     /** - * Decryption -      */ the     functionDecrypt$hashKey) { - Wuyi         $authCodeKey= ' KHUVFB9PIJNYCYMGZDZQEKALYG7DH '; the         $sessionId=$this->authcode ($this->base64url_decode ($hashKey),$this-authcodekey); -         $sessionId=$this->base64url_decode ($hashKey); Wu         return $sessionId; -     } About  $     //URL transfer requires replacement of some characters -     functionBase64url_encode ($data) { -         return RTrim(STRTR(Base64_encode($data), ' +/', '-_ '), ' = '); -     } A     //URL transfer requires replacement of some characters +     functionBase64url_decode ($data) { the         return Base64_decode(Str_pad(STRTR($data, '-_ ', ' +/'),strlen($data)% 4, ' = ',str_pad_right)); -     } $}
Test code:
1 $uId= ' Gouge ';2 $signKey= ' gouge-test123 ';3 $timestamp= Time();4 5 //values that need to be encrypted are added according to the actual situation6 $signParam=Array($uId,$timestamp,$signKey);7 $sessionId=implode(‘,‘,$signParam);8 9 $e=NewTest ();Ten //Encrypt One $r=$e->encrypt ($sessionId); A //decryption - $t=$e->decrypt ($r); -  the Echo $r; - Echo"<br/>"; - Echo $t;
Output Result:

1, encryption = "Z291z2usmtq5odc5ntmxnixnb3vnzs10zxn0mtiz

2, decryption = "gouge,1498795316,gouge-test123

PHP Reversible encryption algorithm

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.