I have written a bit of code below that allows for the creation of accounts. I'd like to add an extra layer of protection by encrypting all data (except for password and username). I have a two questions:
1. Is Openssl the best php encryption practice?
2. How would I add openssl to my code?
I'm having a bit of difficulty integrating openssl with my prepared statement code.
My code:
<?php
session_start();
require_once './config/config.php';
require_once 'includes/auth_validate.php';
//Only super admin is allowed to access this page
if ($_SESSION['admin_type'] !== 'super') {
// show permission denied message
echo 'Permission Denied';
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$admin_type = mysqli_real_escape_string($conn, $_POST['admin_type']);
$position = mysqli_real_escape_string($conn, $_POST['position']);
$first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
$last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
$user_name = mysqli_real_escape_string($conn, $_POST['user_name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$phone_number = mysqli_real_escape_string($conn, $_POST['phone_number']);
$passwd = mysqli_real_escape_string($conn, $_POST['passwd']);
$about = mysqli_real_escape_string($conn, $_POST['about']);
//Error handlers
//Check for empty fields
if (empty($admin_type) || empty($position) || empty($first_name) || empty($last_name) || empty($user_name) || empty($passwd)){
$_SESSION['failure'] = "Admin was not created, missing imporant details!";
header('location: admin_users');
exit();
} else {
$sql = "SELECT * FROM admin_accounts WHERE user_name='$user_name'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
$_SESSION['failure'] = "Admin was not created, username already used!";
header('location: admin_users');
exit();
} else {
//Hashing password
$hashedPasswd = password_hash($passwd, PASSWORD_DEFAULT);
//Insert the user into the database
$sql = "INSERT INTO admin_accounts (admin_type, position, first_name, last_name, user_name, email, phone_number, passwd, about) VALUES (?,?,?,?,?,?,?,?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL Error";
} else {
mysqli_stmt_bind_param($stmt, "sssssssss", $admin_type, $position, $first_name, $last_name, $user_name, $email, $phone_number, $hashedPasswd, $about);
mysqli_stmt_execute($stmt);
{
$_SESSION['success'] = "Admin user added successfully!";
header('location: admin_users');
exit();
}
}
}
}
}
$edit = false;
Openssl_Encryption Example:
<?php
//$key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes
$plaintext = "message to be encrypted";
$cipher = "aes-128-gcm";
if (in_array($cipher, openssl_get_cipher_methods()))
{
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
//store $cipher, $iv, and $tag for decryption later
$original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
echo $original_plaintext."\n";
}
My Attempt at encrypting First_Name only: (This does not work, no effect in database)
<?php
session_start();
require_once './config/config.php';
require_once 'includes/auth_validate.php';
//ONLY SUPER ADMINS ARE ALLOWED TO ACCESS THIS PAGE
if ($_SESSION['admin_type'] !== 'super') {
// show permission denied message
echo 'Permission Denied';
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$admin_type = mysqli_real_escape_string($conn, $_POST['admin_type']);
$position = mysqli_real_escape_string($conn, $_POST['position']);
$first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
$last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
$user_name = mysqli_real_escape_string($conn, $_POST['user_name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$phone_number = mysqli_real_escape_string($conn, $_POST['phone_number']);
$passwd = mysqli_real_escape_string($conn, $_POST['passwd']);
$about = mysqli_real_escape_string($conn, $_POST['about']);
//EROOR HANDLERS
//CHECK FOR EMPTY FIELDS
if (empty($admin_type) || empty($position) || empty($first_name) || empty($last_name) || empty($user_name) || empty($passwd)){
$_SESSION['failure'] = "Admin was not created, missing imporant details!";
header('location: admin_users');
exit();
} else {
$sql = "SELECT * FROM admin_accounts WHERE user_name='$user_name'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
$_SESSION['failure'] = "Admin was not created, username already used!";
header('location: admin_users');
exit();
} else {
//HASHING PASSWORD
$hashedPasswd = password_hash($passwd, PASSWORD_DEFAULT);
//INSERT THE USER INTO THE DATABASE
$sql = "INSERT INTO admin_accounts (admin_type, position, first_name, last_name, user_name, email, phone_number, passwd, about) VALUES (?,?,?,?,?,?,?,?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL Error";
} else {
mysqli_stmt_bind_param($stmt, "sssssssss", $admin_type, $position, $first_name, $last_name, $user_name, $email, $phone_number, $hashedPasswd, $about);
mysqli_stmt_execute($stmt);
{
$first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
$cipher = "aes-128-gcm";
if (in_array($cipher, openssl_get_cipher_methods()))
{
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext = openssl_encrypt($first_name, $cipher, $key, $options=0, $iv, $tag);
}
}
{
$_SESSION['success'] = "Admin user added successfully!";
header('location: admin_users');
exit();
}
}
}
}
}
$edit = false;
?>