How do I Store and read PublicKey from data base using blob or binary in php with example
To store a public key in a MySQL database using PHP, you can follow the steps below:
1.Generate the public key: You can generate a public key using a key generation library in PHP like OpenSSL or Sodium.
2.Encode the public key: Once you have the public key, you need to encode it in a format that can be stored in the database. You can use base64 encoding to convert the binary data to ASCII format.
3.Store the public key in the database: You can then store the encoded public key in a field in the MySQL database.
Here's an example code snippet that demonstrates how to generate a public key using OpenSSL and store it in a MySQL database:
// Generate the public key
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create a private/public key pair
$keys = openssl_pkey_new($config);
// Extract the public key
$public_key = openssl_pkey_get_details($keys)['key'];
// Encode the public key
$encoded_public_key = base64_encode($public_key);
// Store the public key in the database
$sql = "INSERT INTO keys (public_key) VALUES ('$encoded_public_key')";
$result = mysqli_query($conn, $sql);
how to store public key in mysql database using php in pem format
To store a public key in PEM format in a MySQL database using PHP, you can follow the steps below:
1.Generate the public key: You can generate a public key using a key generation library in PHP like OpenSSL or Sodium.
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create a private/public key pair
$keys = openssl_pkey_new($config);
// Extract the public key
$public_key = openssl_pkey_get_details($keys)['key'];
2.Export the public key to PEM format: Once you have the public key, you need to export it to PEM format using the openssl_pkey_export function.
// Export the public key in PEM format
openssl_pkey_export($keys, $pem_public_key);
3.Store the public key in the database: You can then store the PEM-encoded public key in a field in the MySQL database using a SQL query.
// Store the public key in the database
$sql = "INSERT INTO keys (public_key) VALUES ('$pem_public_key')";
$result = mysqli_query($conn, $sql);
Here's an example code snippet that demonstrates how to generate a public key using OpenSSL, export it in PEM format, and store it in a MySQL database:
// Generate the public key
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create a private/public key pair
$keys = openssl_pkey_new($config);
// Extract the public key
$public_key = openssl_pkey_get_details($keys)['key'];
// Export the public key in PEM format
openssl_pkey_export($keys, $pem_public_key);
// Store the public key in the database
$sql = "INSERT INTO keys (public_key) VALUES ('$pem_public_key')";
$result = mysqli_query($conn, $sql);
In this example, we generate a 2048-bit RSA key pair using OpenSSL and extract the public key. We then export the public key in PEM format using the openssl_pkey_export function and store it in a MySQL database using a SQL query. Note that you should always sanitize and validate user input to prevent SQL injection attacks.
// Generate the public key
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create a private/public key pair
$keys = openssl_pkey_new($config);
// Extract the public key
$public_key = openssl_pkey_get_details($keys)['key'];
// Export the public key in PEM format
openssl_pkey_export($keys, $pem_public_key);
// Store the public key in the database
$sql = "INSERT INTO keys (public_key) VALUES ('$pem_public_key')";
$result = mysqli_query($conn, $sql);
=================================================================
Route::post('/generate-keys', function (Request $request) {
// Generate a new public/private key pair
$key_pair = openssl_pkey_new(array(
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
));
// Get the private key string
openssl_pkey_export($key_pair, $private_key);
// Get the public key string
$public_key = openssl_pkey_get_details($key_pair);
$public_key = $public_key["key"];
// Use the private key to sign some data
$email = $request->input('email_address');
$admin_id = $request->input('admin_id');
$admin_email = $request->input('admin_email');
$u_org_slugname = $request->input('u_org_slugname');
$u_org_organization_id = $request->input('u_org_organization_id');
$u_org_role_id = $request->input('u_org_role_id');
$input = $request->all();
log::info("yanha daya axfs");
log::info($input);
$merge = $email.'.pem';
$merge1 = $email.'de'.'.pem';
$myModel = new Mywebaccesstoken;
$myModel->token = $merge;
$myModel->admin_id = $admin_id;
$myModel->pubkey = $public_key;
$myModel->admin_email = $admin_email;
$myModel->u_org_slugname = $u_org_slugname;
$myModel->u_org_organization_id = $u_org_organization_id;
$myModel->u_org_role_id = $u_org_role_id;
$myModel->save();
$data = "Hello, world!";
openssl_sign($data, $signature, $private_key, OPENSSL_ALGO_SHA256);
// $file_path = '~/Downloads/public_key.pub';
// Session::flush();
// session()->put($file_path, $private_key);
// session()->put($email, $public_key);
// Use the public key to verify the signature
$result = openssl_verify($data, $signature, $public_key, OPENSSL_ALGO_SHA256);
file_put_contents($merge, $public_key);
file_put_contents($merge1, $private_key);
// download function start here
ob_clean();
header('Content-Description: File Transfer');
header('Content-Type: application/x-pem-file');
header("Content-Disposition: attachment; filename=public.pem");
exit(readfile('privatekey.pem'));
})->name('generate-keys');
How do I Store and read PublicKey from data base using blob or binary in php with example
1.Storing public or private keys in a BLOB or BINARY column in a database using PHP involves the following steps:
2.Generate the key pair: Use a PHP library such as OpenSSL to generate the public and private key pair.
3.Convert the keys to binary format: Convert the keys to binary format using the openssl_pkey_export() function. This function exports the private key in PKCS#8 format and the public key in X.509 format.
Prepare the SQL statement: Prepare the SQL statement to insert the keys into the BLOB or BINARY column. For example:
$sql = "INSERT INTO my_table (public_key, private_key) VALUES (:public_key, :private_key)";
4.Bind the parameters: Bind the binary data to the prepared statement using the bindParam() or bindValue() function. For example:
<?php
// Generate the key pair
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$keyPair = openssl_pkey_new($config);
// Convert the keys to binary format
openssl_pkey_export($keyPair, $privateKey);
$publicKey = openssl_pkey_get_details($keyPair);
$publicKey = $publicKey['key'];
// Prepare the SQL statement
$sql = "INSERT INTO my_table (public_key, private_key) VALUES (:public_key, :private_key)";
$stmt = $pdo->prepare($sql);
// Bind the parameters
$stmt->bindParam(':public_key', $publicKey, PDO::PARAM_LOB);
$stmt->bindParam(':private_key', $privateKey, PDO::PARAM_LOB);
// Execute the statement
$stmt->execute();
?>
Note that the above example assumes that you have already established a connection to the database using PDO. Also, make sure to properly handle errors and sanitize user inputs to prevent SQL injection attacks.
Top comments (0)