Practical PHP shared with public key encryption class (different encryption results every time), php encrypted

Source: Internet
Author: User

Practical PHP shared with public key encryption class (different encryption results every time), php encrypted

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. This is the biggest highlight and 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 <strlen ($ txt ); $ 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 <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 ++;} 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 <strlen ($ txt ); $ 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 getPK () {return $ this-> key ;}}

Usage:

<? Php // contains the Encryption Class require_once ('jiami. class. php '); // the string to be encrypted $ string = 'HTTP: // www.bkjia.com'; // 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 '<meta charset = "UTF-8"/> '; echo 'before encryption :'. $ string. '<br/>'; echo 'encrypted :'. $ enc. '<br/>'; echo 'After decryption :'. $ 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.


What are the differences between symmetric key encryption technology and asymmetric key encryption technology?

Symmetric encryption is faster than asymmetric encryption. symmetric encryption keys cannot be made public, while asymmetric private keys must be kept public. It is complicated to manage and publish symmetric encryption, DES, AES, and IDEA are usually used for symmetric encryption of algorithms. Asymmetric RSA

Which of the following statements about asymmetric key encryption is true?

C. Identical encryption keys and encryption keys
 

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.