非对称加密之 RSA
1、生成证书和获取公钥、私钥
$configargs = [
'config' => 'E:\wamp64\bin\apache\apache2.4.23\conf\openssl.cnf'
];
$resource = openssl_pkey_new($configargs);
openssl_pkey_export($resource, $private_key, null, $configargs);
$detail = openssl_pkey_get_details($resource);
$public_key = $detail['key'];
openssl genrsa -out rsa_private_key.pem
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
2、加密和解密
openssl_public_encrypt($data, $encrypted, $public_key);
base64_encode($encrypted);
openssl_public_decrypt(base64_decode($data), $decrypted, $public_key);
openssl_private_encrypt($data, $encrypted, $private_key);
base64_encode($encrypted);
openssl_private_decrypt(base64_decode($data), $decrypted, $private_key);