php基于openssl的rsa加密解密示例,php错误日志简单

作者: 新金沙平台  发布:2019-07-12

本文实例讲述了php基于openssl的rsa加密解密。分享给大家供大家参考,具体如下:

本文实例分析了PHP进行批量任务处理不超时的解决方法。分享给大家供大家参考,具体如下:

本文实例讲述了php配置错误日志的方法。分享给大家供大家参考,具体如下:

<?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";
?>

PHP批量任务处理

php.ini:

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

PHP在批量处理任务的时候会超时,其实解决方法很简单了,就是把任务分割,一次处理一部分,任务进度可以放在服务端也可以放在客户端,不是很复杂的话放在客户端,用js来处理就可以了.

; 错误日志
log_errors = On
; 显示错误
display_errors = Off
; 日志路径
error_log = "/usr/local/lnmp/php/var/log/error_log"
; 错误等级
error_reporting = E_ALL&~E_NOTICE

密码安全性在线检测:

客户端js回调处理

php-fpm.conf:

高强度密码生成器:
http://tools.jb51.net/password/CreateStrongPassword

客户端处理的时候需要住一个地方,就是使用ajax处理的时候,ajax是异步的,使用for循环来处理的时候只是批量请求,这样任务量大的时候会直接DDOS服务器,所以需要等待回调函数返回,然后进行下一次的请求.

[global]
; php-fpm pid文件
pid = /usr/local/php/var/run/php-fpm.pid
; php-fpm 错误日志路径
error_log = /usr/local/php/var/log/php-fpm.log
; php-fpm 记录错误日志等级
log_level = notice
[www]
; 记录错误到php-fpm的日志中
;catch_workers_output = yes
; 慢日志
slowlog = var/log/slow.log
; 关闭打印日志
php_flag[display_errors] = off
; 错误日志
php_admin_value[error_log] = /usr/local/php/var/log/www.log
; 记录错误
php_admin_flag[log_errors] = on
; 内存使用量
php_admin_value[memory_limit] = 32M

本文由新金沙平台发布于新金沙平台,转载请注明出处:php基于openssl的rsa加密解密示例,php错误日志简单

关键词: 新金沙平台