Home  >  Article  >  Backend Development  >  How to generate public and private keys in php (code)

How to generate public and private keys in php (code)

不言
不言Original
2018-08-23 10:23:356340browse

The content of this article is about how PHP generates public and private keys (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.


<?php
//http://www.lampol-blog.com/detail/aid/ZDk5MmFNZ2pJL1pROW5QZU9KZ2FWdVlFTDVHRnRmZm4rNDMzSFlHNg%3D%3D 各种秘钥生成的教程
//生成密钥
$opensslConfigPath = "D:\phpstudy\PHPTutorial\Apache\conf\openssl.cnf";	
$config = array(
    "digest_alg" => "sha512",
    "private_key_bits" =>2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
	&#39;config&#39;=> $opensslConfigPath
);
 
 
//创建密钥对
$res = openssl_pkey_new($config);
//生成私钥
openssl_pkey_export($res, $privkey, null, $config);
//生成公钥
$pubKey = openssl_pkey_get_details($res)[&#39;key&#39;];
print_r($privkey);
echo &#39;<p style="height:100px;background:red">1</p>&#39;;
print_r($pubKey);
file_put_contents(&#39;private.key&#39;,$privkey);
file_put_contents(&#39;public.key&#39;,$pubKey);
?>

Related recommendations:

php rsa how to generate public and private keys

PHP uses public and private keys for high-strength encryption

The above is the detailed content of How to generate public and private keys in php (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn