PHP寫的加密函數,支援私人密鑰(詳細介紹)_PHP教程

來源:互聯網
上載者:User
在開發PHP系統時,會員部分往往是一個必不可少的模組,而密碼的處理又是不得不面對的問題,PHP 的 Mcrypt 加密庫又需要額外設定,很多人都是直接使用md5()函數加密,這個方法的確安全,但是因為md5是無法復原加密,無法還原密碼,因此也有一些不便之處,本文介紹加密函數支援私密金鑰,用起來還是不錯的.
代碼如下:
PHP:
複製代碼 代碼如下:





  1. // 說明:PHP 寫的加密函數,支援私人密鑰


  2. // 整理:http://www.jb51.net




  3. function
    keyED(
    $txt
    ,$encrypt_key
    )


  4. {


  5. $encrypt_key
    = md5
    (
    $encrypt_key
    )
    ;

  6. $ctr
    =0
    ;

  7. $tmp
    = ""
    ;

  8. for
    (
    $i
    =0
    ;$i
    (
    $txt
    )
    ;$i
    ++)


  9. {


  10. if
    (
    $ctr
    ==strlen
    (
    $encrypt_key
    )
    )
    $ctr
    =0
    ;

  11. $tmp
    .= substr
    (
    $txt
    ,$i
    ,1
    )
    ^ substr
    (
    $encrypt_key
    ,$ctr
    ,1
    )
    ;

  12. $ctr
    ++;

  13. }


  14. return
    $tmp
    ;

  15. }




  16. function
    encrypt(
    $txt
    ,$key
    )


  17. {


  18. srand
    (
    (
    double)
    microtime
    (
    )
    *1000000
    )
    ;

  19. $encrypt_key
    = md5
    (
    rand
    (
    0
    ,32000
    )
    )
    ;

  20. $ctr
    =0
    ;

  21. $tmp
    = ""
    ;

  22. for
    (
    $i
    =0
    ;$i
    (
    $txt
    )
    ;$i
    ++)


  23. {


  24. if
    (
    $ctr
    ==strlen
    (
    $encrypt_key
    )
    )
    $ctr
    =0
    ;

  25. $tmp
    .= substr
    (
    $encrypt_key
    ,$ctr
    ,1
    )
    . (
    substr
    (
    $txt
    ,$i
    ,1
    )
    ^ substr
    (
    $encrypt_key
    ,$ctr
    ,1
    )
    )
    ;

  26. $ctr
    ++;

  27. }


  28. return
    keyED(
    $tmp
    ,$key
    )
    ;

  29. }




  30. function
    decrypt(
    $txt
    ,$key
    )


  31. {


  32. $txt
    = keyED(
    $txt
    ,$key
    )
    ;

  33. $tmp
    = ""
    ;

  34. for
    (
    $i
    =0
    ;$i
    (
    $txt
    )
    ;$i
    ++)


  35. {


  36. $md5
    = substr
    (
    $txt
    ,$i
    ,1
    )
    ;

  37. $i
    ++;

  38. $tmp
    .= (
    substr
    (
    $txt
    ,$i
    ,1
    )
    ^ $md5
    )
    ;

  39. }


  40. return
    $tmp
    ;

  41. }




  42. $key
    = "www.yitu.org"
    ;

  43. $string
    = "我是加密字元"
    ;



  44. // encrypt $string, and store it in $enc_text


  45. $enc_text
    = encrypt(
    $string
    ,$key
    )
    ;



  46. // decrypt the encrypted text $enc_text, and store it in $dec_text


  47. $dec_text
    = decrypt(
    $enc_text
    ,$key
    )
    ;



  48. print
    "加密的 text : $enc_text
    "
    ;

  49. print
    "解密的 text : $dec_text
    "
    ;

  50. ?>






每一次加密後的結果是不一樣的,大大加強了密碼的安全性.

http://www.bkjia.com/PHPjc/327520.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327520.htmlTechArticle在開發PHP系統時,會員部分往往是一個必不可少的模組,而密碼的處理又是不得不面對的問題,PHP 的 Mcrypt 加密庫又需要額外設定,很多人都是直...

  • 聯繫我們

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