Implementation tips for PHP encryption and decryption

Source: Internet
Author: User
This article mainly introduces the PHP encryption and decryption of the relevant implementation skills, interested in the reference of friends, hope to help you.

Specific as follows:

<?phpclass ender{private $enkey;//encryption decryption with the key private $rep _char= ' # ';  Replace the = in the encrypted base64 string, because = is forbidden in some cases,//this can be replaced with an allowable character.    The construction parameter is the key public function __construct ($key = ") {if (! $key) {$this->enkey= $key;  }}//Set key http://blog.ddian.cn public function Set_key ($key) {$this->enkey= $key;   } Private 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;  }//Encrypt string public function encrypt ($txt, $key = ") {if (! $key) {$key = $this->enkey;   } 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 + +; } $r =base64_encode ($this-&Gt;keyed ($tmp, $key));  $r =str_replace (' = ', $this->rep_char, $r);   return $r;  }//Decrypt the string public function decrypt ($txt, $key = ') {$txt =str_replace ($this->rep_char, ' = ', $txt);  $txt =base64_decode ($txt);  if (! $key) {$key = $this->enkey;   } $txt = $this->keyed ($txt, $key);   $tmp = "";   for ($i =0; $i <strlen ($txt); $i + +) {$MD 5 = substr ($txt, $i, 1);   $i + +;   $tmp. = (substr ($txt, $i, 1) ^ $md 5);   } return $tmp; }}

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP using the Magickwand module to manipulate the image to add a watermark method

The function of using curl to forge IP in PHP

PHP for the current encoding of the judgment and corresponding code conversion implementation techniques

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.