Practical PHP with public key cryptography class sharing (every time encryption results are different OH), PHP encryption _php Tutorial

Source: Internet
Author: User
Tags asymmetric encryption

Useful PHP with public key cryptography class sharing (every time encryption results are different OH), PHP encryption


Web Interactive security has always been a major site of the first solution, this article introduces the PHP encryption class is very practical oh, with the public key, this is the biggest bright spot, no public key is not decrypted, plus density is very high.

Class Code:

<?php/** * PHP Encryption class * Qiong Tai 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 (0,32000));    $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; } }
 
    

   
  
 

How to use:

<?php//first contains the encryption class require_once (' jiami.class.php '); The string to encrypt $string = ' http://www.bkjia.com '; Instantiate the cryptographic class $jiami= new Jiami (); Set the public key $jiami->setkey (' QTTC '); Encrypted string $enc = $jiami->encrypt ($string, $jiami->getpk ()); Decrypt string $dec = $jiami->decrypt ($enc, $jiami->getpk ()); Echo '
 
   ; Echo ' before encryption: '. $string. '
'; Echo ' after encryption: '. $enc. '
'; Echo ' after decryption: '. $dec;? >

Page Execution Results

Result 1:

Result 2:

As you can see from the above results, each encryption string generated is different, which is random.
Decryption requires the use of the public key when encrypting, otherwise it cannot be decrypted. If you encrypt the public key using ' QTTC ', when decrypting, also need to use this ' QTTC ' as the public key to decrypt, otherwise cannot decrypt.


What are the different manifestations of symmetric key cryptography and asymmetric key encryption technology?

Symmetric encryption is faster than asymmetric encryption, the symmetric encryption key cannot be exposed, and the asymmetric private key must be public, the public key can be disclosed, about the management and release symmetric encryption is more complex, about the algorithm symmetric encryption is usually used Des,aes,idea. RSA for asymmetric use

The following is true for asymmetric key encryption

C. Encryption keys and decryption keys are the same

http://www.bkjia.com/PHPjc/866662.html www.bkjia.com true http://www.bkjia.com/PHPjc/866662.html techarticle practical PHP with Public key cryptography class sharing (every time encryption results are different OH), PHP encrypted web interaction Security has always been a major site of the first solution, this article introduces the PHP encryption class is very real ...

  • Related Article

    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.