如何创建新的密钥对并将其保存在文件中?

问题描述:

如何创建一个新的密钥对并将它们保存在文件中?我猜是OpenSSL。我有Windows 7和Xampp,它在APache目录中有OpenSSL(尽管我在openssl_pkey_new()方面有一些问题(请参阅Why does openssl_pkey_new() fail?如何创建新的密钥对并将其保存在文件中?

无论如何,一旦我配置了OpenSSL,代码看起来像创建新的密钥对,并将它们保存在文件中

要生成密钥对:

<?php 
/* Create the private and public key */ 
$res = openssl_pkey_new(); 

/* Extract the private key from $res to $privKey */ 
openssl_pkey_export($res, $privKey); 

/* Extract the public key from $res to $pubKey */ 
$pubKey = openssl_pkey_get_details($res); 
$pubKey = $pubKey["key"]; 
?> 

要保存的关键目标文件:

file_put_contents($file, $key);