PHP Write encryption function, support private key (detailed introduction) _php Tutorial

Source: Internet
Author: User
Tags mcrypt php write
In the development of PHP system, the member part is often an essential module, and the password processing has to face the problem, PHP Mcrypt Encryption Library needs additional settings, many people are directly using the MD5 () function encryption, this method is indeed safe, but because MD5 is irreversible encryption, Unable to restore the password, so there are some inconveniences, this article describes the encryption function to support the private key, it is good to use.
The code is as follows:
Php:
Copy CodeThe code is as follows:





  1. Description: PHP write encryption function, support private key


  2. Finishing: 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. $MD 5
    = substr
    (
    $txt
    ,$i
    ,1
    )
    ;

  37. $i
    ++;

  38. $tmp
    .= (
    Substr
    (
    $txt
    ,$i
    ,1
    )
    ^ $MD 5
    )
    ;

  39. }


  40. return
    $tmp
    ;

  41. }




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

  43. $string
    = "I am an encrypted character"
    ;



  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
    "Encrypted text: $enc _text
    "
    ;

  49. Print
    "decrypted text: $dec _text
    "
    ;

  50. ?>






The results of each encryption are not the same, greatly enhancing the security of the password.

http://www.bkjia.com/PHPjc/327520.html www.bkjia.com true http://www.bkjia.com/PHPjc/327520.html techarticle in the development of PHP system, the member part is often an essential module, and the password processing has to face the problem, PHP Mcrypt Encryption Library needs additional settings, many people are straight ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.