discuz論壇程式的PHP加密函數原理

來源:互聯網
上載者:User

   康盛的 authcode 函數可以說對中國的PHP界作出了重大貢獻。包括康盛自己的產品,以及大部分中國使用PHP的公司都用這個函數進行加密,authcode 是使用異或運算進行加密和解密。

  原理如下,假如:

  加密

  明文:1010 1001

  密匙:1110 0011

  密文:0100 1010

  得出密文0100 1010,解密之需和密匙異或下就可以了

  解密

  密文:0100 1010

  密匙:1110 0011

  明文:1010 1001

  並沒有什麼高深的演算法,密匙重要性很高,所以,關鍵在於怎麼產生密匙。

  那我們一起看下康盛的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. // 密匙

  11. $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);

  12.

  13. // 密匙a會參與加解密

  14. $keya = md5(substr($key, 0, 16));

  15. // 密匙b會用來做資料完整性驗證

  16. $keyb = md5(substr($key, 16, 16));

  17. // 密匙c用於變化產生的密文

  18. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length):

  substr(md5(microtime()), -$ckey_length)) : '';

  19. // 參與運算的密匙

  20. $cryptkey = $keya.md5($keya.$keyc);

  21. $key_length = strlen($cryptkey);

  22. // 明文,前10位用來儲存時間戳記,解密時驗證資料有效性,10到26位用來儲存$keyb(密匙b),解密時會通過這個密匙驗證資料完整性

  23. // 如果是解碼的話,會從第$ckey_length位開始,因為密文前$ckey_length位儲存 動態密匙,以保證解密正確

  24. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) :

  sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;

  25. $string_length = strlen($string);

  26. $result = '';

  27. $box = range(0, 255);

  28. $rndkey = array();

  29. // 產生密匙簿

  30. for($i = 0; $i <= 255; $i++) {

  31. $rndkey[$i] = ord($cryptkey[$i % $key_length]);

  32. }

  33. // 用固定的演算法,打亂密匙簿,增加隨機性,好像很複雜,實際上對並不會增加密文的強度

  34. for($j = $i = 0; $i < 256; $i++) {

  35. $j = ($j + $box[$i] + $rndkey[$i]) % 256;

  36. $tmp = $box[$i];

  37. $box[$i] = $box[$j];

  38. $box[$j] = $tmp;

  39. }

  40. // 核心加解密部分

  41. for($a = $j = $i = 0; $i < $string_length; $i++) {

  42. $a = ($a + 1) % 256;

  43. $j = ($j + $box[$a]) % 256;

  44. $tmp = $box[$a];

  45. $box[$a] = $box[$j];

  46. $box[$j] = $tmp;

  47. // 從密匙簿得出密匙進行異或,再轉成字元

  48. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));

  49. }

  50. if($operation == 'DECODE') {

  51. // substr($result, 0, 10) == 0 驗證資料有效性

  52. // substr($result, 0, 10) - time() > 0 驗證資料有效性

  53. // substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) 驗證資料完整性

  54. // 驗證資料有效性,請看未加密明文的格式

  55. if((substr($result, 0, 10) == 0 substr($result, 0, 10) - time() > 0) &&

  substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {

  56. return substr($result, 26);

  57. } else {

  58. return '';

  59. }

  60. } else {

  61. // 把動態密匙儲存在密文裡,這也是為什麼同樣的明文,生產不同密文後能解密的原因

  62. // 因為加密後的密文可能是一些特殊字元,複製過程可能會丟失,所以用base64編碼

  63. return $keyc.str_replace('=', '', base64_encode($result));

  64. }

  65. }

  但是有點遺憾,這個函數所有權屬於康盛創想,並不能自由使用的




相關文章

聯繫我們

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