Php encryption and decryption class (including public key) implementation code .? PhpclassCrypt {Source: Longge blog protected $ key; public key privatefunctionkeyED ($ txt, $ encrypt_key) {$ encrypt_keymd5 ($ encrypt_key); $ ctr0; $ tmp; for ($ I Class Crypt {
// Source: Longge blog
Protected $ key = ""; // public key
Private 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;
}
Public function encrypt ($ txt, $ key = "")
{
If (empty ($ key )){
$ Key = $ this-> 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 $ this-> keyED ($ tmp, $ key );
}
Public function decrypt ($ txt, $ key = "")
{
If (empty ($ key )){
$ Key = $ this-> key;
}
$ Txt = $ this-> keyED ($ txt, $ key );
$ Tmp = "";
For ($ I = 0; $ I {
$ Md5 = substr ($ txt, $ I, 1 );
$ I ++;
$ Tmp. = (substr ($ txt, $ I, 1) ^ $ md5 );
}
Return $ tmp;
}
Public function setKey ($ key)
{
If (empty ($ key )){
Return null;
}
$ This-> key = $ key;
}
Public function getKey ()
{
Return $ this-> key;
}
// Downloads By http://www.bkjia.com
}
$ String = "http://www.52blogger.com ";
$ Crypt = new Crypt ();
$ Crypt-> setKey (http://www.52blogger.com ");
$ Enc_text = $ crypt-> encrypt ($ string, $ crypt-> getKey ());
$ Dec_text = $ crypt-> decrypt ($ enc_text, $ crypt-> getKey ());
Echo "before encryption: $ string
\ N ";
Echo "encrypted: $ enc_text
\ N ";
Echo "after decryption: $ dec_text \ n ";
?>
Execution result:
Pre-encryption: http://www.52blogger.com
After encryption: hszq'z * kP ~ Y (WpVs = 6q1_zbr5qkhtz (f = Zm
Decrypted: http://www.52blogger.com
Note: Each encrypted string is different. In addition, the public key is required for decryption. if the public key is incorrect, the decryption result is inconsistent with the plaintext. unfortunately, this method does not seem to support Chinese ~
Http://www.bkjia.com/PHPjc/363850.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/363850.htmlTechArticle? Php class Crypt {// source of this article: Longge blog protected $ key =; // public key private function keyED ($ txt, $ encrypt_key) {$ encrypt_key = md5 ($ encrypt_key ); $ ctr = 0; $ tmp =; for ($ I =...