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.