PHP based OpenSSL RSA encryption and decryption example _php tips

Source: Internet
Author: User
Tags begin rsa private key decrypt openssl openssl rsa php programming

The example in this article describes the RSA encryption decryption based on OpenSSL in PHP. Share to everyone for your reference, specific as follows:

 <?php $config = Array (//"config" => "d:/phpserver/lighttpd/openssl.cnf",//"
Config ' => ' d:/phpstudy/lighttpd/openssl.cnf ', ' private_key_bits ' => 1024,//Size of key.
' Private_key_type ' => openssl_keytype_rsa);
$res = Openssl_pkey_new ();
$res = Openssl_pkey_new ($config);
Get private key//Openssl_pkey_export ($res, $privkey, "passphrase number 1");
Openssl_pkey_export ($res, $privkey);
Var_dump ($privkey);
Get public key $pubkey =openssl_pkey_get_details ($res);
echo "------------><br/>";
Print_r ($pubkey ["RSA"]);
$bin _str= $pubkey ["RSA"] ["N"];
Print_r ($bin _str);
echo "<br/>";
echo $bin _hex_str = Pack ("h*", Bin2Hex ($bin _str));
echo $bin _hex_str = Bin2Hex ($bin _str);
 echo "<br/>------------<<br/>";
$pubkey = $pubkey ["Key"];
Var_dump ($privkey);
Var_dump ($pubkey); echo $privkey. "
<br/><br/> "; echo $pubkey. " <br/><br/> ";?> 
<?php $private _key = '-----BEGIN RSA private Key-----miicxqibaakbgqdnsq4ampze01mpeo9dabrdss0xwxkekefuowjnefg+ Na6p52js Isw79qynfqwm7akiappzu0+kkwjtsxidqfuv5y2vaa3w2tre5n51ucwuk6/lfzhx bg0d+ Bnlzy6kb6bpgpgvbhaknbvjbcs0v9h4v3wutlr9qauj05q8qe70sqidaqab aogab/ 9az1mmknimfbiilxk1avvn353qy51ynytxffhbyisikvsyqtusuyffbjh7 ezyz8uws1jxuqb847iuz/ejzzpvev480tv9yejn0dhj4yw6y/
KPJWO/9KECET6TK VATXH+WGMIESZKXE0KUVK41GPV7A4GWWQJ6P4VJX+QKTANECQQDUO7ENW10DZXLG ynkquhgkdi9npjrlhcgz4rw0uorqusggqnvkaqpy8bajokkxwiq0avtpbiwo8k0+ s50rgnynakea3dgxy9owrc65vqs/
Uxv28pyle2gy0xsvwa97svnvyjj58ingevys VEZ3GRTNAZPBPKCRUCS6NIDK761OT5A0NQJAOSRO1PY4QQP5LSBDOJRLQ7DJAKEF 6YJCKBRVSHLIIHJQVDKD8KOEPZAQRHL3AUVE3K9PTKEZ2KWJJH0I0DKMHQJBAMCM f+7wnwa0pebj0mvqfyiechb/tjwxm/lskbnp17tv6gy/ HWOXGWMLA15DLIO2CMJZ Dbihby8y+nu7k4vadzecqqco018o5tjwl+o+bwyzv1er603yp+jvjiskuptuyzvs NIz+ClwspGmdUwPWlLqr5JfKm+
OFPALIEPFJINSCGWPP-----End RSA PRIVATE KEY-----'; $private _key = '-----BEGIN RSA private Key-----//miicxaibaakbgqdvfoce2nebi+4fyvs36j1SHHHVUSR0DXXE9NZAA/BIH6NODYWH//cqo2lyco6osvy+csspewjjyatrctq/ri6c/am64cju+hm1lzh1gh05c2kvj2dvex//gcdsz+/ Xphdxykaq/hw9s1neyr2x4ejdpynyucln8xwgelrnljunbl/saqidaqab//aogak2oz28hkkb+dornt2zktv2de85jhpr1ea+zuj+ 1m2xhjkzlqjiybb6m53lts//j446yzope/kxhgpwwijlq9wp1kizpeforwzz1wmuj8koq95vcxbeuyjx4ez4dio6// Vmr9babfuoah0gaq8ktjoz0durcecwv+ii7ilanorjpfwuucqqdveqrle2/n8niy//p7yvnniaiasl/e+xyqxcmidsdedqznrjhjfdobqpv/ TGJNUAN9ZBVHNNLBLNI5NF//2nqgrh5jakea48gqmbo9koonvo//q581jsbkfga4i7detq80od4kyt6fwezkaehx//k64sjmpfD/ Ys2hekfhyndo/k0nsuxglnwwjbakpx7/u9qllybnsfrwalhy9oc5at//hi0ilneoz5r43lngb+jcf/aa6uw357wb2wzpzdwuxokfofuukv+ Hekbj00ucqcxd//wowsiqcob5rwcbrevr49fotxmc7dq3omw3gu+odbb2izwkabah9cticexwfx5mks//
bs5rxzne3awmnnrpqnscqddocbxkf5ycthqlmeetfkodvzg69cozgykdlenoxk6g//qnnat2y2mxinyglpij+tfxxfiqeshdph4s/lw6t2isy=
-----End RSA PRIVATE KEY-----';
$public _key = '-----BEGIN public Key-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdnsq4ampze01mpeo9dabrdss0x Wxkekefuowjnefg+na6p52jsisw79qynfqwm7akiappzU0+KKWJTSXIDQFUV5Y2V Aa3w2tre5n51ucwuk6/lfzhxbg0d+bnlzy6kb6bpgpgvbhaknbvjbcs0v9h4v3wu TLR9QauJ05Q8qe70sQIDAQAB--
---End public KEY-----';
$private _key= $privkey;
$public _key= $pubkey;
$FP =fopen ("Private_key", "w");
Fwrite ($fp, $private _key);
Fclose ($FP);
$FP =fopen ("Public_key", "w");
Fwrite ($fp, $public _key);
Fclose ($FP);
echo $private _key; $pi _key = openssl_pkey_get_private ($private _key);//This function can be used to determine whether the private key is available, the available return resource ID Resource ID $pu _key = openssl_pkey_get_
Public ($public _key);//This function can be used to determine whether the key is available Print_r ($pi _key); echo "\ n";
Print_r ($pu _key), echo "\ n";
echo "<br/>----------------------<br/>"; 
$data = "Woshizhu";//Original Data $encrypted = "";
$decrypted = "";
echo "Source data:", $data, "<br/>";
echo "Private key encrypt:\n"; Openssl_private_encrypt ($data, $encrypted, $pi _key);//private key Encryption $encrypted = Base64_encode ($encrypted);//encrypted content usually contains special characters,
Need to encode the conversion, in the network transmission through the URL to pay attention to whether the Base64 encoding is URL-safe echo "<br/>----------private key encryption------------<br/>";
Echo $encrypted, "<br/>"; echo "<br/>----------Private key encryption------------<br/> ";
echo "Public key decrypt:\n";
$decrypted = "";
Openssl_public_decrypt (Base64_decode ($encrypted), $decrypted, $pu _key);//private key encrypted content can be decrypted through the public key echo $decrypted, "\ n";
echo "---------------------------------------\ n";
echo "Public key encrypt:\n";
Openssl_public_encrypt ($data, $encrypted, $pu _key);//public key Encryption $encrypted = Base64_encode ($encrypted);
echo $encrypted, "\ n";
$decrypted = ""; $encrypted = "jbeapcp9iwwyjyelgqtrzxfxm4wvkcasn/ojz7njfr23o76fdbxexpf+ Pgwacw3petdobwl4108wr3ihkmy2iykiexgjbyyvx2w9ahies8zsoip3ljimhytm93rr8sc5xxhwqc3dhbq16jwhyz2d+
Rrophd4x84gf3jxwivrgo4= ";
echo "Private key decrypt:\n"; Openssl_private_decrypt (Base64_decode ($encrypted), $decrypted, $pi _key);//private key decryption echo "---:". $decrypted, "\ n";

 >

PS: About encryption and decryption interested friends can also refer to the site online tools:

Password Security online detection:
Http://tools.jb51.net/password/my_password_safe

High Strength Password Generator:
Http://tools.jb51.net/password/CreateStrongPassword

MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL encryption/decryption tool:
Http://tools.jb51.net/password/urlrethunder

Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

More about PHP related content to view the site topics: "PHP Encryption Method Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary (including Word,excel, Access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with your PHP programming.

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.