自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP加密解密函數(shù)authcode的具體使用方法分析

開發(fā) 后端
對于初學(xué)者來說,在PHP實際編程中進(jìn)行數(shù)據(jù)加密,可能還是比較困難的。我們今天將要為大家介紹的就是PHP加密解密函數(shù)authcode的具體使用方法。

 PHP語言也有保證數(shù)據(jù)安全的函數(shù)存在,他們的合理運用可以幫助我們實現(xiàn)數(shù)據(jù)加密功能,提高程序的安全性。我們今天要向大家介紹的PHP加密解密函數(shù)authcode,就是能夠?qū)崿F(xiàn)這一功能的函數(shù)。

#t#PHP加密解密函數(shù)authcode的具體示例代碼如下:

  1. // 參數(shù)解釋   
  2. // $string: 明文 或 密文   
  3. // $operation:DECODE表示解密,其它表示加密   
  4. // $key: 密匙   
  5. // $expiry:密文有效期   
  6. function authcode($string, $operation = 
    'DECODE', $key = '', $expiry = 0) {   
  7. // 動態(tài)密匙長度,相同的明文會生成不同密文就是依靠動態(tài)密匙   
  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會用來做數(shù)據(jù)完整性驗證   
  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加密解密函數(shù)authcode參與運算的密匙   
  18. $cryptkey = $keya.md5($keya.$keyc);   
  19. $key_length = strlen($cryptkey);   
  20. // 明文,前10位用來保存時間戳,解密時驗證數(shù)據(jù)有效性,
    10到26位用來保存$keyb(密匙b),解密時會通過這個密匙驗證數(shù)據(jù)完整性   
  21. // 如果是解碼的話,會從第$ckey_length位開始,因為密文前$ckey_
    length位保存 動態(tài)密匙,以保證解密正確   
  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加密解密函數(shù)authcode產(chǎn)生密匙簿   
  28. for($i = 0; $i <= 255; $i++) {   
  29. $rndkey[$i] = ord($cryptkey[$i % $key_length]);   
  30. }   
  31. // 用固定的算法,打亂密匙簿,增加隨機性,好像很復(fù)雜,
    實際上對并不會增加密文的強度   
  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加密解密函數(shù)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加密解密函數(shù)authcode從密匙簿得出密匙進(jìn)行異或,再轉(zhuǎn)成字符   
  46. $result .chr(ord($string[$i]) ^ (
    $box[($box[$a] + $box[$j]) % 256]));   
  47. }   
  48. if($operation == 'DECODE') {   
  49. // substr($result, 0, 10) == 0 驗證數(shù)據(jù)有效性   
  50. // substr($result, 0, 10) - time() > 0 驗證數(shù)據(jù)有效性   
  51. // substr($result, 10, 16) == substr(md5(substr
    ($result, 26).$keyb), 0, 16) 驗證數(shù)據(jù)完整性   
  52. // 驗證數(shù)據(jù)有效性,請看未加密明文的格式   
  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加密解密函數(shù)authcode把動態(tài)密匙保存在密文里,這也是為什么同樣的明文,
    生產(chǎn)不同密文后能解密的原因   
  60. // 因為加密后的密文可能是一些特殊字符,
    復(fù)制過程可能會丟失,所以用base64編碼   
  61. return $keyc.str_replace('=', '', 
    base64_encode($result));   
  62. }   
  63. }  

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

責(zé)任編輯:曹凱 來源: 百度博客
相關(guān)推薦

2009-12-07 16:52:59

PHP函數(shù)getima

2009-11-26 19:05:04

PHP函數(shù)explod

2009-11-26 15:23:24

PHP函數(shù)ereg()

2009-12-01 19:02:20

PHP取整函數(shù)

2009-12-02 18:51:12

PHP分頁類

2009-11-24 16:18:14

PHP5析構(gòu)函數(shù)

2009-11-24 19:25:32

PHP關(guān)聯(lián)數(shù)組

2009-11-25 10:02:27

PHP會話Sessio

2009-12-01 17:00:49

PHP變量

2009-11-24 15:50:09

PHP上傳類uploa

2009-11-30 17:43:54

PHP split()

2009-12-04 14:23:33

PHP JSON加密函

2009-12-01 18:02:41

PHP表單數(shù)組

2009-11-16 16:54:00

PHP構(gòu)造函數(shù)

2009-11-30 18:08:30

PHP制作動態(tài)計數(shù)器

2009-11-26 18:49:54

PHP函數(shù)preg_s

2009-11-16 15:40:58

PHP數(shù)組函數(shù)

2011-07-12 17:18:23

PHPstrtotime

2009-11-24 18:23:26

PHP函數(shù)array_

2009-12-08 13:18:17

點贊
收藏

51CTO技術(shù)棧公眾號