PHP加密解密函數authcode的具體使用方法分析_PHP教程

來源:互聯網
上載者:User
PHP加密解密函數authcode的具體範例程式碼如下:

  1. // 參數解釋
  2. // $string: 明文 或 密文
  3. // $operation:DECODE表示解密,其它表示加密
  4. // $key: 密匙
  5. // $expiry:密文有效期間
  6. function authcode($string, $operation =
    'DECODE', $key = '', $expiry = 0) {
  7. // 動態密匙長度,相同的明文會產生不同密文就是依靠動態密匙
  8. $ckey_length = 4;
  9. // 密匙
  10. $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
  11. // 密匙a會參與加解密
  12. $keya = md5(substr($key, 0, 16));
  13. // 密匙b會用來做資料完整性驗證
  14. $keyb = md5(substr($key, 16, 16));
  15. // 密匙c用於變化產生的密文
  16. $keyc = $ckey_length ? ($operation == 'DECODE'
    ? substr($string, 0, $ckey_length): substr(md5
    (microtime()), -$ckey_length)) : '';
  17. //PHP加密解密函數authcode參與運算的密匙
  18. $cryptkey = $keya.md5($keya.$keyc);
  19. $key_length = strlen($cryptkey);
  20. // 明文,前10位用來儲存時間戳記,解密時驗證資料有效性,
    10到26位用來儲存$keyb(密匙b),解密時會通過這個密匙驗證資料完整性
  21. // 如果是解碼的話,會從第$ckey_length位開始,因為密文前$ckey_
    length位儲存 動態密匙,以保證解密正確
  22. $string = $operation == 'DECODE' ? base64_decode(substr
    ($string, $ckey_length)) : sprintf('%010d', $expiry ?
    $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  23. $string_length = strlen($string);
  24. $result = '';
  25. $box = range(0, 255);
  26. $rndkey = array();
  27. //PHP加密解密函數authcode產生密匙簿
  28. for($i = 0; $i <= 255; $i++) {
  29. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  30. }
  31. // 用固定的演算法,打亂密匙簿,增加隨機性,好像很複雜,
    實際上對並不會增加密文的強度
  32. for($j = $i = 0; $i < 256; $i++) {
  33. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  34. $tmp = $box[$i];
  35. $box[$i] = $box[$j];
  36. $box[$j] = $tmp;
  37. }
  38. //PHP加密解密函數authcode核心加解密部分
  39. for($a = $j = $i = 0; $i < $string_length; $i++) {
  40. $a = ($a + 1) % 256;
  41. $j = ($j + $box[$a]) % 256;
  42. $tmp = $box[$a];
  43. $box[$a] = $box[$j];
  44. $box[$j] = $tmp;
  45. // PHP加密解密函數authcode從密匙簿得出密匙進行異或,再轉成字元
  46. $result .= chr(ord($string[$i]) ^ (
    $box[($box[$a] + $box[$j]) % 256]));
  47. }
  48. if($operation == 'DECODE') {
  49. // substr($result, 0, 10) == 0 驗證資料有效性
  50. // substr($result, 0, 10) - time() > 0 驗證資料有效性
  51. // substr($result, 10, 16) == substr(md5(substr
    ($result, 26).$keyb), 0, 16) 驗證資料完整性
  52. // 驗證資料有效性,請看未加密明文的格式
  53. if((substr($result, 0, 10) == 0 ||
    substr($result, 0, 10) - time() > 0) &&
    substr($result, 10, 16) == substr(md5
    (substr($result, 26).$keyb), 0, 16)) {
  54. return substr($result, 26);
  55. } else {
  56. return '';
  57. }
  58. } else {
  59. //PHP加密解密函數authcode把動態密匙儲存在密文裡,這也是為什麼同樣的明文,
    生產不同密文後能解密的原因
  60. // 因為加密後的密文可能是一些特殊字元,
    複製過程可能會丟失,所以用base64編碼
  61. return $keyc.str_replace('=', '',
    base64_encode($result));
  62. }
  63. }

以上代碼就是PHP加密解密函數authcode的具體使用方法,希望大家能夠通過這篇文章介紹的內容初步掌握這個函數的含義。


http://www.bkjia.com/PHPjc/446263.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446263.htmlTechArticlePHP加密解密函數authcode的具體範例程式碼如下: //參數解釋 //$string:明文或密文 //$operation:DECODE表示解密,其它表示加密 //$key:密匙 //$expir...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.