"sha512", "private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA, ); // Create the private and public key $res = openssl_pkey_new( $config ); $outfilename = 'examplekeys.pem'; // export private & public key to file openssl_pkey_export_to_file ( $res , $outfilename ); // Extract the private key from $res to $privKey openssl_pkey_export( $res, $privKey ); echo '
Private key : ' . $privKey . '' . PHP_EOL; // Extract the public key from $res to $pubKey $pubKey = openssl_pkey_get_details( $res ); $pubKey = $pubKey["key"]; echo '
Public key : ' . $pubKey . '' . PHP_EOL; $data = 'plaintext data goes here'; echo '
The data : ' . $data . '' . PHP_EOL; // Encrypt the data to $encrypted using the public key openssl_public_encrypt( $data, $encrypted, $pubKey ); echo '
Encrypted data : ' . base64_encode( $encrypted ) . '' . PHP_EOL; // Decrypt the data using the private key and store the results in $decrypted openssl_private_decrypt( $encrypted , $decrypted, $privKey ); echo '
Decrypted data : ' . $decrypted . '' . PHP_EOL;