Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new key pair and save them in files?

Tags:

php

openssl

xampp

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?

like image 479
Mawg says reinstate Monica Avatar asked Nov 27 '10 02:11

Mawg says reinstate Monica


People also ask

How do I save SSH key pairs?

Create a New SSH Key Pair Generating public/private rsa key pair. Enter file in which to save the key (/home/username/. ssh/id_rsa): Press enter to save your keys to the default /home/username/.

What command can you use to generate a key pair?

Run the ssh-keygen command. You can use the -t option to specify the type of key to create. The command prompts you to enter the path to the file in which you want to save the key. A default path and file name are suggested in parentheses.


1 Answers

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);
like image 136
i.amniels Avatar answered Sep 22 '22 14:09

i.amniels