<?php
//生成私钥文件:肯定要使用个密码呗
//openssl genrsa -out rsa_private_key.pem 1024 -passout pass:123456
//利用私钥,生成公钥:
//openssl rsa -in rsa_private_key.pem -passin pass:111111 -pubout -out rsa_public_key.pem
$private_key = file_get_contents("./rsa_private_key.pem");
$public_key = file_get_contents("./rsa_public_key.pem");
$pi_key = openssl_pkey_get_private($private_key);// 可用返回资源id
$pu_key = openssl_pkey_get_public($public_key);
// 加密数据
$data = array(
'id' => '1234567890',
'name' => 'xxxx',
'mobile' => '13512341234',
);
$data = json_encode($data);
$encrypted = '';
$decrypted = '';
//公钥加密 私钥解密
// openssl_public_encrypt($data, $encrypted, $pu_key);//公钥加密
// $encrypted = base64_encode($encrypted);// base64传输
// echo $encrypted,"<br/>";
// openssl_private_decrypt(base64_decode($encrypted), $decrypted, $pi_key);//私钥解密
// echo $decrypted,"<br/>";
// print_r(json_decode($decrypted, true));
//私钥加密 公钥解密
openssl_private_encrypt($data, $encrypted, $pi_key);//公钥加密
$encrypted = base64_encode($encrypted);// base64传输
echo $encrypted,"<br/>";
openssl_public_decrypt(base64_decode($encrypted), $decrypted, $pu_key);//私钥解密
echo $decrypted,"<br/>";
print_r(json_decode($decrypted, true));