Skip to content

Instantly share code, notes, and snippets.

@devops-school
Created January 22, 2023 20:10
Show Gist options
  • Select an option

  • Save devops-school/7d5e73d327f85d45c57d6e215923e1bb to your computer and use it in GitHub Desktop.

Select an option

Save devops-school/7d5e73d327f85d45c57d6e215923e1bb to your computer and use it in GitHub Desktop.
Encryption using RSA public and Private Key using PHP
Write a php code for generating public key and private key on page one and on page two encrypt data through input by user of public key and on page three decrypt data by uploading private key
<?php
// Get the private key from the file
$privateKeyStr = file_get_contents("private_key.pem");
// Get the encrypted data from the file
$encryptedData = file_get_contents("encrypted_data.bin");
// Decrypt the data using the private key
openssl_private_decrypt($encryptedData, $decryptedData, $privateKeyStr);
// Output the decrypted data
echo "Decrypted Data: " . $decryptedData . "\n";
?>
Write a php code for generating public key and private key on page one and on page two encrypt data through input by user of public key and on page three decrypt data by uploading private key
<?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);
// Save the encrypted data to a file
file_put_contents("encrypted_data.bin", $encryptedData);
?>
Write a php code for generating public key and private key and enrcrypt data using public key and decrypt using private key
----------------------------------
<?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"];
// Data to encrypt
$data = "secret message";
// Encrypt the data using the public key
openssl_public_encrypt($data, $encryptedData, $publicKey);
// Decrypt the data using the private key
openssl_private_decrypt($encryptedData, $decryptedData, $privateKeyStr);
// Output the original data and the decrypted data
echo "Original Data: " . $data . "\n";
echo "Decrypted Data: " . $decryptedData . "\n";
?>
Write a php code for generating public key and private key
----------------------------------
<?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"];
// Use $privateKeyStr and $publicKey as needed
?>
Write a php code for generating public key and private key on page one and on page two encrypt data through input by user of public key and on page three decrypt data by uploading private key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment