国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP中使用OpenSSL生成證書及加密解密

2024-09-04 11:49:33
字體:
來源:轉載
供稿:網友

依賴于OpenSSL擴展:

  1. /*加密解密*/ 
  2. functionauthcode($string,$operation='E') { 
  3.   $ssl_public=file_get_contents(DATA_PATH."/conf/cert_public.key"); 
  4.   $ssl_private=file_get_contents(DATA_PATH."/conf/cert_private.pem"); 
  5.   $pi_key= openssl_pkey_get_private($ssl_private);//這個函數可用來判斷私鑰是否是可用的,可用返回資源id Resource id 
  6.   $pu_key= openssl_pkey_get_public($ssl_public);//這個函數可用來判斷公鑰是否是可用的 
  7.   if(false == ($pi_key||$pu_key))return'證書錯誤' 
  8.   $data=""
  9.   if($operation=='D'){ 
  10.     openssl_private_decrypt(base64_decode($string),$data,$pi_key);//私鑰解密 
  11.   }else
  12.     openssl_public_encrypt($string,$data,$pu_key);//公鑰加密 
  13.     $data=base64_encode($data); 
  14.   } 
  15.   return$data
  16. /*生成證書*/ 
  17. functionexportOpenSSLFile(){ 
  18.   $config=array
  19.     "digest_alg"   =>"sha512"
  20.     "private_key_bits"=> 4096,          //字節數  512 1024 2048  4096 等 
  21.     "private_key_type"=> OPENSSL_KEYTYPE_RSA,  //加密類型 
  22.   ); 
  23.   $res= openssl_pkey_new($config); 
  24.   if($res== false)returnfalse; 
  25.   openssl_pkey_export($res,$private_key); 
  26.   $public_key= openssl_pkey_get_details($res); 
  27.   $public_key=$public_key["key"]; 
  28.   file_put_contents(DATA_PATH."/conf/cert_public.key",$public_key); 
  29.   file_put_contents(DATA_PATH."/conf/cert_private.pem",$private_key); 
  30.   openssl_free_key($res); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱芜市| 乐业县| 额尔古纳市| 都江堰市| 调兵山市| 嘉禾县| 曲阜市| 河东区| 监利县| 宜兰市| 清远市| 丰顺县| 尼勒克县| 轮台县| 山阳县| 宜宾市| 高陵县| 东平县| 麻城市| 旌德县| 六安市| 黑山县| 镇巴县| 垦利县| 宜兰县| 大庆市| 汨罗市| 修文县| 尼玛县| 兴隆县| 上栗县| 桃源县| 辰溪县| 大悟县| 贵德县| 浑源县| 札达县| 滁州市| 苍溪县| 祥云县| 专栏|