This article mainly introduces the practical PHP shared Encryption Class with public key. This class achieves different encryption results each time, but there is no problem with decryption. It is a very practical Encryption Class, for more information, see
This article mainly introduces the practical PHP shared Encryption Class with public key. This class achieves different encryption results each time, but there is no problem with decryption. It is a very practical Encryption Class, for more information, see
WEB interaction security has always been the primary solution for every major website. The PHP Encryption Class introduced in this article is very practical. It has a public key, which is the biggest highlight. It cannot be decrypted without a public key, the addition density is very high.
Class Code:
<? Php/*** PHP Encryption class * qiongtai blog */class Jiami {// public key protected $ key = 'Lee '; private function keyED ($ txt, $ encrypt_key) {$ encrypt_key = md5 ($ encrypt_key); $ ctr = 0; $ tmp = ''; for ($ I = 0; $ I Key;} srand (double) microtime () * 1000000); $ encrypt_key = md5 (rand (); $ ctr = 0; $ tmp = ''; for ($ I = 0; $ I KeyED ($ tmp, $ key);} public function decrypt ($ txt, $ key = '') {if (empty ($ key )) {$ key = $ this-> key;} $ txt = $ this-> keyED ($ txt, $ key); $ tmp = ''; for ($ I = 0; $ I Key = $ key;} public function getPK () {return $ this-> key ;}}
Usage:
<? Php // contains the Encryption Class require_once ('jiami. class. php '); // the string to be encrypted $ string = 'HTTP: // www.jb51.net'; // instantiate the Encryption Class $ jiami = new Jiami (); // set the Public Key $ jiami-> setKey ('qttc '); // encrypted string $ enc = $ jiami-> encrypt ($ string, $ jiami-> getPK ()); // decrypt the string $ dec = $ jiami-> decrypt ($ enc, $ jiami-> getPK (); echo' '; Echo' before encryption: '. $ string .'
'; Echo 'encrypted:'. $ enc .'
'; Echo': '. $ dec;?>
Page execution result
Result 1:
Result 2:
From the above results, we can see that each encryption produces different encrypted strings, which are random.
You must use the public key to decrypt the data. Otherwise, the data cannot be decrypted. For example, if you use 'qttc 'as the encryption public key, you also need to use this 'qttc' as the public key for decryption. Otherwise, the decryption will fail.