PHP code encryption and decryption method

Source: Internet
Author: User

<? Php
// $ Key = "This is supposed to be a secret key !!! "; It is estimated that the password is overcast.
$ Key = "this is a secret ";
Function keyED ($ txt, $ encrypt_key)
{
$ Encrypt_key = md5 ($ encrypt_key );
$ Ctr = 0;
$ Tmp = "";
For ($ I = 0; $ I <strlen ($ txt); $ I)
{
If ($ ctr = strlen ($ encrypt_key) $ ctr = 0;
$ Tmp. = substr ($ txt, $ I, 1) ^ substr ($ encrypt_key, $ ctr, 1 );
$ Ctr;
}
Return $ tmp;
}

Function encrypt ($ txt, $ key)
{
Srand (double) microtime () * 1000000 );
$ Encrypt_key = md5 (rand (0, 32000 ));
$ Ctr = 0;
$ Tmp = "";
For ($ I = 0; $ I <strlen ($ txt); $ I)
{
If ($ ctr = strlen ($ encrypt_key) $ ctr = 0;
$ Tmp. = substr ($ encrypt_key, $ ctr, 1 ).
(Substr ($ txt, $ I, 1) ^ substr ($ encrypt_key, $ ctr, 1 ));
$ Ctr;
}
Return keyED ($ tmp, $ key );
}

Function decrypt ($ txt, $ key)
{
$ Txt = keyED ($ txt, $ key );
$ Tmp = "";
For ($ I = 0; $ I <strlen ($ txt); $ I)
{
$ Md5 = substr ($ txt, $ I, 1 );
$ I;
$ Tmp. = (substr ($ txt, $ I, 1) ^ $ md5 );
}
Return $ tmp;
}

$ String = "strings to be encrypted, such as passwords .";

// Start encryption encrypt $ string, and store it in $ enc_text
$ Enc_text = encrypt ($ string, $ key );

// Start decrypting decrypt the encrypted text $ enc_text, and store it in $ dec_text
$ Dec_text = decrypt ($ enc_text, $ key );

Print "original string: $ string <Br> n ";
Print "encrypted string: $ enc_text <Br> n ";
Print "decrypted string: $ dec_text <Br> n ";
?>

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.