Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Created September 9, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niraj-shah/81877fcf5eca02b08f50 to your computer and use it in GitHub Desktop.
Save niraj-shah/81877fcf5eca02b08f50 to your computer and use it in GitHub Desktop.
Create Private and Public Keys using PHP
<?php
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$result = openssl_pkey_new( $config );
// Extract the private key from $result to $privKey
openssl_pkey_export( $result, $privKey );
// Extract the public key from $result to $pubKey
$pubKey = openssl_pkey_get_details( $result );
$pubKey = $pubKey["key"];
// save private key to file
file_put_contents( 'private.key', $privKey );
// save public key to file
file_put_contents( 'public.pub', $pubKey );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment