How to implement encryption in php and how to implement decryption. It is actually very simple. Php has many functions for everyone. Today, I will write a simple piece of code for you.
How to implement encryption in php and how to implement decryption. It is actually very simple. Php has many functions for everyone. Today, I will write a simple piece of code for you. The code is as follows:
The following is a code snippet:
$ Key = "www.phpxd.com !!! ";
Function keyED ($ txt, $ encrypt_key)
{
$ Encrypt_key = md5 ($ encrypt_key );
$ Ctr = 0;
$ Tmp = "";
For ($ I = 0; $ 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
{
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
{
$ Md5 = substr ($ txt, $ I, 1 );
$ I ++;
$ Tmp. = (substr ($ txt, $ I, 1) ^ $ md5 );
}
Return $ tmp;
}
Test:
$ String = "phpxd.com !!! ";
// Encrypt and give the encrypted value to $ enc_text
$ Enc_text = encrypt ($ string, $ key );
// Decrypt and give the encrypted value to $ dec_text
$ Dec_text = decrypt ($ enc_text, $ key );
// Print:
Print "Original text: $ string
";
Print "Encrypted text: $ enc_text
";
Print "Decrypted text: $ dec_text
";
?>