PHP OpenSSL RSA Encryption decryption

Source: Internet
Author: User
Tags begin rsa private key openssl rsa
$config = Array (
"Config" = "d:/phpserver/lighttpd/openssl.cnf",
' Config ' = ' d:/phpstudy/lighttpd/openssl.cnf ',
' Private_key_bits ' = 1024x768,//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 "------------;
";
Print_r ($pubkey ["RSA"]);
//$bin _str= $pubkey ["RSA"] ["N"];
//Print_r ($bin _str);
//echo "
";
echo $bin _hex_str = Pack ("h*", Bin2Hex ($bin _str));
//echo $bin _hex_str = Bin2Hex ($bin _str);
//echo "
------------<
";

$pubkey = $pubkey ["Key"];
//Var_dump ($privkey);
//Var_dump ($pubkey); The
Echo $privkey.

";
echo $pubkey. "

";
?


!--? 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 if the private key is available, return the resource ID Resource ID
$pu _key = openssl_pkey_get_public ($public _key);//This function can be used to determine whether the public key is available
Print_r ($pi _key); echo "\ n";
Print_r ($pu _key); echo "\ n";
echo "
----------------------
";

$data = "Woshizhu";//RAW data
$encrypted = "";
$decrypted = "";

echo "Source data:", $data, "
";

echo "Private key encrypt:\n";

Openssl_private_encrypt ($data, $encrypted, $pi _key);//private key encryption
$encrypted = Base64_encode ($encrypted);//The contents of the encryption usually contain special characters, need to encode the conversion, the transmission between the network through the URL to pay attention to base64 encoding is URL-safe
echo "
----------private key Encryption------------
";
Echo $encrypted, "
";
echo "
----------private key Encryption------------
";
echo "Public key decrypt:\n";
$decrypted = "";
Openssl_public_decrypt (Base64_decode ($encrypted), $decrypted, $pu _key);//private key encrypted content can be decrypted by 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";
?>

The above describes the PHP OpenSSL RSA encryption and decryption, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.