PHP implements enhanced version encryption and decryption instances and enhanced version encryption and decryption

Source: Internet
Author: User

PHP implements enhanced version encryption and decryption instances and enhanced version encryption and decryption

This example describes how PHP implements enhanced version encryption and decryption. Share it with you for your reference. The details are as follows:

<? Phpclass Ender {private $ enkey; // key used for encryption and decryption private $ rep_char = '#'; // replace = In the encrypted base64 string, because = is not allowed in some cases, // here a allowed character can be used as a replacement. // The construction parameter is the key public function _ construct ($ key = '') {if (! $ Key) {$ this-> enkey = $ key;} // set the 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 ;} // encryption string public function encrypt ($ txt, $ ke Y = '') {if (! $ Key) {$ key = $ this-> enkey;} srand (double) microtime () * 1000000); $ encrypt_key = md5 (rand )); $ 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-> keyED ($ tmp, $ key); $ r = str_replace ('=', $ this-> rep_char, $ r ); return $ r;} // decrypt the public String 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 ++) {$ md5 = substr ($ txt, $ I, 1 ); $ I ++; $ tmp. = (substr ($ txt, $ I, 1) ^ $ md5);} return $ tmp ;}}

I hope this article will help you with php programming.

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.