This article introduces php custom encryption and decryption. Many of our friends use php's own. What if we write it ourselves? Let's look at the code below.
| The Code is as follows: |
Copy code |
<? Php
// Description: encryption function written in PHP. Private keys are supported.
// Finishing: http://www.bKjia. c0m
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;
}
$ Key = "YITU.org ";
$ String = "I am an encrypted character ";
// Encrypt $ string, and store it in $ enc_text
$ Enc_text = encrypt ($ string, $ key );
// Decrypt the encrypted text $ enc_text, and store it in $ dec_text
$ Dec_text = decrypt ($ enc_text, $ key );
Print "encrypted text: $ enc_text <Br> ";
Print "decrypted text: $ dec_text <Br> ";
?>
|