php基于openssl的rsa加密解密示例
程序员文章站
2024-04-02 15:49:10
本文实例讲述了php基于openssl的rsa加密解密。分享给大家供大家参考,具体如下:
本文实例讲述了php基于openssl的rsa加密解密。分享给大家供大家参考,具体如下:
<?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);//这个函数可用来判断私钥是否是可用的,可用返回资源id resource id $pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的 print_r($pi_key);echo "\n"; print_r($pu_key);echo "\n"; echo "<br />----------------------<br />"; $data = "woshizhu";//原始数据 $encrypted = ""; $decrypted = ""; echo "source data:",$data,"<br />"; echo "private key encrypt:\n"; openssl_private_encrypt($data,$encrypted,$pi_key);//私钥加密 $encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的 echo "<br />----------私钥加密------------<br />"; echo $encrypted,"<br />"; echo "<br />----------私钥加密------------<br />"; echo "public key decrypt:\n"; $decrypted=""; openssl_public_decrypt(base64_decode($encrypted),$decrypted,$pu_key);//私钥加密的内容通过公钥可用解密出来 echo $decrypted,"\n"; echo "---------------------------------------\n"; echo "public key encrypt:\n"; openssl_public_encrypt($data,$encrypted,$pu_key);//公钥加密 $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);//私钥解密 echo "--->:".$decrypted,"\n"; ?>
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
密码安全性在线检测:
高强度密码生成器:
http://tools.jb51.net/password/createstrongpassword
md5在线加密工具:
http://tools.jb51.net/password/createmd5password
迅雷、快车、旋风url加密/解密工具:
在线散列/哈希算法加密工具:
更多关于php相关内容可查看本站专题:《php加密方法总结》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。