PHP encrypts data using OpenSSL

Source: Internet
Author: User
Tags decrypt

1<?PHP2 /**3 * Created by Phpstorm.4 * User:hanks5 * date:6/2/20176 * time:6:03 PM7  */8 //using PHP functions to generate a key pair9 The //OPENSSL module provides a number of OpenSSL-related functions, and the reference manual generates key pairs in the following ways:Ten $privateKey=openssl_pkey_new ([ One' Private_key_bits ' = 2048,//size of private key A' Private_key_type ' = Openssl_keytype_rsa, - ]); -  theOpenssl_pkey_export_to_file ($privateKey, ' Php-private.key '); - $key= Openssl_pkey_get_details ($privateKey); - file_put_contents(' Php-public.key ',$key[' Key ']); -  +Openssl_free_key ($privateKey);//Freeing Resources
1<?PHP2 /**3 * Created by Phpstorm.4 * User:hanks5 * date:6/8/20176 * time:12:20 PM7  */8 //encrypt data using a key pair9 //Use the public key generated by the PHP function of the first step to fragment (chunk) The block of plaintext and then encrypt it (in real-life, all text can be encrypted directly):Ten //$plain = ' This is a test data '; One $plain= [ A0=>[ -' 0 ' = ' SD ', -' 1 ' = ' ' Make ' the], -1=>[ -' 0 ' = ' sd2 ', -' 1 ' = + ' makes 2 ' +], - ]; + Echo' Plian text: '. Json_encode ($plain,true); A $plain=gzcompress(Json_encode ($plain,true));//Compress Data at $pubkeyStr=file_get_contents('./php-public.key '); - $publicKey= Openssl_pkey_get_public ($pubkeyStr); -  - $p _key= Openssl_pkey_get_details ($publicKey); - $chunkSize=Ceil($p _key[' bits ']/8)-11;//I don't know why.-11, add explanation later. -  in $output= ' '; -  to  while($plain) { +     $chunk=substr($plain, 0,$chunkSize); -     $plain=substr($plain,$chunkSize); the  *     $encrypted= ' '; $     if(!openssl_public_encrypt ($chunk,$encrypted,$publicKey)) {Panax Notoginseng          die("Failed to encrypt data"); -     } the     $output.=$encrypted; + } AOpenssl_free_key ($publicKey); the $output=Base64_encode($output); + Echo' Encrypted: '. ($output); - file_put_contents('./enc.data ',$output);
1<?PHP2 /**3 * Created by Phpstorm.4 * User:hanks5 * date:6/8/20176 * time:12:22 PM7  */8 //Decrypt Data9 //Use the private key to decrypt the data:Ten $keyStr=file_get_contents('./php-private.key '); One if(!$privateKey= Openssl_pkey_get_private ($keyStr)) { A      die(' Get private key failed '); - } -  the $encrypted=file_get_contents('./enc.data '); - Echo' Encrypted data: '.$encrypted; -  - $encrypted=Base64_decode($encrypted); +  - $p _key= Openssl_pkey_get_details ($privateKey); + $chunkSize=Ceil($p _key[' bits ']/8); A $output= ' '; at  -  while($encrypted) { -     $chunk=substr($encrypted, 0,$chunkSize); -     $encrypted=substr($encrypted,$chunkSize); -     $decryptd= ' '; -     if(!openssl_private_decrypt ($chunk,$decryptd,$privateKey)) { in          die(' failed to decrypt data '); -     } to     $output.=$decryptd; + } -Openssl_free_key ($privateKey); the $output=gzuncompress($output); * Echo"\ndecrypted Data:"; $ Var_dump(Json_decode ($output,true));

PHP encrypts data using OpenSSL

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.