php 可逆的密碼編譯演算法

來源:互聯網
上載者:User

標籤:add   class   trunc   turn   結果   加密   input   substr   length   

代碼:
 1 <?php 2  3 class Test { 4  5     private $authCodeKey = ‘khUvFB9pijNyCYMGZdzqeKalyg7dh‘;     6  7     function authCode($input, $key) { 8  9         # Input must be of even length.10         if (strlen($input) % 2) {11             //$input .= ‘0‘;12         }13 14         # Keys longer than the input will be truncated.15         if (strlen($key) > strlen($input)) {16             $key = substr($key, 0, strlen($input));17         }18 19         # Keys shorter than the input will be padded.20         if (strlen($key) < strlen($input)) {21             $key = str_pad($key, strlen($input), ‘0‘, STR_PAD_RIGHT);22         }23 24         # Now the key and input are the same length.25         # Zero is used for any trailing padding required.26 27         # Simple XOR‘ing, each input byte with each key byte.28         $result = ‘‘;29         for ($i = 0; $i < strlen($input); $i++) {30             $result .= $input{$i} ^ $key{$i};31         }32         return $result;33     }34 35     /**36      * 加密37      */38     function encrypt($sessionId) {39 40         $hashKey = $this->base64url_encode($this->authCode($sessionId, $this->authCodeKey));41         $hashKey = $this->base64url_encode($sessionId);42         return $hashKey;43     }44 45 46     /**47      * 解密48      */49     function decrypt($hashKey) {50 51         $authCodeKey = ‘khUvFB9pijNyCYMGZdzqeKalyg7dh‘;52         $sessionId = $this->authCode($this->base64url_decode($hashKey), $this->authCodeKey);53         $sessionId = $this->base64url_decode($hashKey);54         return $sessionId;55     }56 57     // url傳輸需要替換部分字元58     function base64url_encode($data) {59         return rtrim(strtr(base64_encode($data), ‘+/‘, ‘-_‘), ‘=‘);60     }61     // url傳輸需要替換部分字元62     function base64url_decode($data) {63         return base64_decode(str_pad(strtr($data, ‘-_‘, ‘+/‘), strlen($data) % 4, ‘=‘, STR_PAD_RIGHT));64     }65 }
測試代碼:
 1 $uId = ‘gouge‘; 2 $signKey = ‘gouge-test123‘; 3 $timestamp = time(); 4  5 // 需要加密的值 根據實際情況添加 6 $signParam = array($uId, $timestamp, $signKey); 7 $sessionId = implode(‘,‘, $signParam); 8  9 $e = new Test();10 // 加密11 $r = $e->encrypt($sessionId);12 // 解密13 $t = $e->decrypt($r);14 15 echo $r;16 echo "<br/>";17 echo $t;
輸出結果:

1、加密   =》Z291Z2UsMTQ5ODc5NTMxNixnb3VnZS10ZXN0MTIz

2、解密   =》gouge,1498795316,gouge-test123

php 可逆的密碼編譯演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.