最新下载
热门教程
- 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); } | |
相关文章
- 无尽花界诸神角色图鉴 无尽花界阵营角色详细介绍 05-08
- 驯龙之旅兑换码大全 驯龙之旅最新可用礼包码汇总 05-08
- 曲奇云盘怎么保存别人分享的文件 05-08
- 末世避难所手游新手入门指南 末世避难所手游零基础快速上手教程 05-08
- 六道轮回手游上线时间揭晓 六道轮回手游正式开服日期分享 05-08
- 无限暖暖埋骨地10个冥想点位一览 05-08