Debug School

rakesh kumar
rakesh kumar

Posted on

how to download publickey and privatekey in speified folder using file_put_contents in php

Sure, here's an example of how to download a public key and a private key in a specified folder using file_put_contents() function in PHP:

$folderPath = "/path/to/keys";

// Save the private key to the specified folder
file_put_contents("$folderPath/private.pem", $privateKey);

// Save the public key to the specified folder
file_put_contents("$folderPath/public.pem", $publicKey);
Enter fullscreen mode Exit fullscreen mode
// Generate a new public/private key pair
$keyPair = openssl_pkey_new([
    "digest_alg" => "sha256",
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
]);

// Get the private key
openssl_pkey_export($keyPair, $privateKey);

// Get the public key
$keyDetails = openssl_pkey_get_details($keyPair);
$publicKey = $keyDetails["key"];

// Define the folder to save the keys in
$folderPath = "/path/to/keys";

// Save the private key to the specified folder
file_put_contents("$folderPath/private.pem", $privateKey);

// Save the public key to the specified folder
file_put_contents("$folderPath/public.pem", $publicKey);
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Top comments (0)