🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Encryption using Public key and Private key in PHP using mySql database


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);
?>
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x