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