RSA算法是现今使用最广泛的公钥密码算法,也是是号称地球上最安全的加密算法,与 md5 和 sha1 不同,到目前为止,也只有极短的RSA加密被破解。
这里我使用的是PHPStudy的集成环境,在它的Apache\conf目录下就有一个openssl.cnf,我们可以直接拿来使用,下面代码希望对大家有所帮助。
<?php
function opensslRsa(){
$config = array(
"config" => "D:\phpStudy\Apache\conf\openssl.cnf", // 路径
"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 ) return false;
//从得到的资源中获取私钥,把私钥赋给$privKey
openssl_pkey_export($res, $privKey, null, $config);
//从得到的资源中获取公钥,返回公钥$pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
file_put_contents("./cert_public.key", $pubKey);
file_put_contents("./cert_private.pem", $privKey);
openssl_free_key($res);
}
将config指向的路径改为你的openssl.cnf的路径,加密类型和字节数可以根据你的需求去更改。
文章不错支持一下吧
这个我只在本地测试用