Write a php code for generating public key and private key on page one and on page two encrypt user entered data using public key and store the encrypted data in mysql database and on page three decrypt encrypted data from mysql database and display on screen.
Here is an example of generating a public and private key pair on page one, encrypting data on page two using the public key, storing the encrypted data in a MySQL database, and then decrypting the data on page three using the private key and displaying it on the screen in PHP:
<?php | |
// Get the private key from the file | |
$privateKeyStr = file_get_contents("private_key.pem"); | |
// Connect to the MySQL database | |
$conn = new mysqli("host", "username", "password", "database"); | |
// Prepare the SQL statement | |
$stmt = $conn->prepare("SELECT encrypted_data FROM data WHERE id = ?"); | |
// Bind the ID to the SQL statement | |
$stmt->bind_param("i", $_GET["id"]); | |
// Execute the SQL statement | |
$stmt->execute(); | |
// Bind the result to the $encryptedData variable | |
$stmt->bind_result($encryptedData); | |
// Get the result | |
$stmt->fetch(); | |
// Decrypt the data using the private key | |
openssl_private_decrypt($encryptedData, $decryptedData, $privateKeyStr); | |
// Output the decrypted data | |
echo "Decrypted Data: " . $decryptedData . "\n"; | |
// Close the MySQL connection | |
$conn->close(); | |
?> |
<?php | |
// Get the public key from the file | |
$publicKey = file_get_contents("public_key.pem"); | |
// Data to encrypt | |
$data = $_POST["data"]; | |
// Encrypt the data using the public key | |
openssl_public_encrypt($data, $encryptedData, $publicKey); | |
// Connect to the MySQL database | |
$conn = new mysqli("host", "username", "password", "database"); | |
// Prepare the SQL statement | |
$stmt = $conn->prepare("INSERT INTO data (encrypted_data) VALUES (?)"); | |
// Bind the encrypted data to the SQL statement | |
$stmt->bind_param("s", $encryptedData); | |
// Execute the SQL statement | |
$stmt->execute(); | |
// Close the MySQL connection | |
$conn->close(); | |
?> |
<?php | |
// Generate a new private key | |
$privateKey = openssl_pkey_new(array( | |
"private_key_bits" => 2048, | |
"private_key_type" => OPENSSL_KEYTYPE_RSA, | |
)); | |
// Extract the private key from $privateKey to $privateKeyStr | |
openssl_pkey_export($privateKey, $privateKeyStr); | |
// Generate a new public key | |
$publicKey = openssl_pkey_get_details($privateKey)["key"]; | |
// Save the private key to a file | |
file_put_contents("private_key.pem", $privateKeyStr); | |
// Save the public key to a file | |
file_put_contents("public_key.pem", $publicKey); | |
?> |
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND