5

How do I create a new key pair and save them in files? OpenSSL I guess. I have Windows 7 and Xampp, which has OpenSSL in the APache directory (although I am having some problems with openssl_pkey_new() (see Why does openssl_pkey_new() fail?).

Anyway, once I get OpenSSL configured, what does the code look like to create a new key pair and save them in files?

Community
  • 1
  • 1
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

1 Answers1

9

To generate a key pair:

<?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"];
?>

To save the key to a target file:

file_put_contents($file, $key);
Samuel Hawksby-Robinson
  • 2,652
  • 4
  • 24
  • 25
i.amniels
  • 1,781
  • 1
  • 25
  • 37