1、代碼如下:
<?php /** * @description 創建RSA 公鑰私鑰 * @return array|bool */ function create_rsa_key(){ //配置信息 $config = array( 'config' => 'C:/soft/php-7.4.22/extras/ssl/openssl.cnf',//找到你的PHP目錄下openssl配置文件 'digest_alg' => 'sha512', 'private_key_bits' => 1024,//指定多少位來生成私鑰 'private_key_type' => OPENSSL_KEYTYPE_RSA ); $res = openssl_pkey_new($config); //獲取私鑰 openssl_pkey_export($res, $private_key, null, $config); //獲取公鑰 $details = openssl_pkey_get_details($res); $public_key = $details['key']; return array('public_key' => $public_key, 'private_key' => $private_key); } ?>
2、生成結果: