最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP中使用OpenSSL生成证书及加密解密
时间:2022-06-24 14:57:36 编辑:袖梨 来源:一聚教程网
依赖于OpenSSL扩展
| 代码如下 | 复制代码 |
/*加密解密*/ functionauthcode($string,$operation='E') { $ssl_public=file_get_contents(DATA_PATH."/conf/cert_public.key"); $ssl_private=file_get_contents(DATA_PATH."/conf/cert_private.pem"); $pi_key= openssl_pkey_get_private($ssl_private);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id $pu_key= openssl_pkey_get_public($ssl_public);//这个函数可用来判断公钥是否是可用的 if(false == ($pi_key||$pu_key))return'证书错误'; $data=""; if($operation=='D'){ openssl_private_decrypt(base64_decode($string),$data,$pi_key);//私钥解密 }else{ openssl_public_encrypt($string,$data,$pu_key);//公钥加密 $data=base64_encode($data); } return$data; } /*生成证书*/ functionexportOpenSSLFile(){ $config=array( "digest_alg" =>"sha512", "private_key_bits"=> 4096, //字节数 512 1024 2048 4096 等 "private_key_type"=> OPENSSL_KEYTYPE_RSA, //加密类型 ); $res= openssl_pkey_new($config); if($res== false)returnfalse; openssl_pkey_export($res,$private_key); $public_key= openssl_pkey_get_details($res); $public_key=$public_key["key"]; file_put_contents(DATA_PATH."/conf/cert_public.key",$public_key); file_put_contents(DATA_PATH."/conf/cert_private.pem",$private_key); openssl_free_key($res); } | |
相关文章
- 抖音充值抖币官网直达-抖音充值抖币全流程详解 03-22
- 有什么好玩的枪战游戏手机版 2026火爆的枪战手游排行榜 03-22
- 艾尔登法环黑夜君临铁之眼主流玩法是什么 03-22
- 夸克网页版入口在哪-夸克浏览器2026官网最新地址 03-22
- 红色沙漠采石场任务完成攻略分享 03-22
- 漫蛙manwa2-免费漫画入口页 03-22