Encryption / Decryption PHP Code

I have created this code block, this can encrypt and dencrypt just about anything, and can prevent sql injection form post will encrypt the data before post, whatever is posted will just become encrypted text and will not allow sql injection.

// Put in secure location
$PrivateKey = “xsfrjdtyrkjdr6t7ikfrd76tki6”; // Random characters
$PublicKey = “hip’ju9o-ji0-]i0-=]i0=”; // Random characters
$password_hash = “xsfytrjft7f7y6l8ig7u9i”; // Random characters
/////////

// require all of the following at root level to use
// auto lock all post using input name
// add more as needed or add to database to make it easy to add more
$fields_array = [
‘username’,
‘password’,
‘email’,
‘name’,
];

foreach ($fields_array as $value) {
$Postvalue = $value[‘fields’];
if (isset($_POST[$Postvalue])) {
$_POST[$Postvalue] = Encrypt::lock($_POST[$Postvalue]);
}
}

// manual lock LockEncrypt(‘whatever’) = dtyrjrdtujfdytu
function LockEncrypt($string) {
return Encrypt::lock($string);
}

// manual unlock UnLockEncrypt(‘dtyrjrdtujfdytu’) = whatever
function UnLockEncrypt($string) {
return Encrypt::unlock($string);
}

class KeyChain {

public static function join_keys() {
    global $PrivateKey;
    global $PublicKey;
    $FullRing = $PrivateKey . $PublicKey;
    return $FullRing;
}

public static function hash_keys() {
    global $password_hash;
    return $password_hash;
}

public static function PrivateKey() {
    global $PrivateKey;
    return $PrivateKey;
}

public static function PublicKey() {
    global $PublicKey;
    return $PublicKey;
}

}

class Encrypt extends KeyChain {

public static function lock($string) {
    $keys = parent::join_keys();
    $hash = parent::hash_keys();
    $key = hash('sha256', $keys);
    $initialization_vector = substr(hash('sha256', $hash), 0, 16);
    $output = openssl_encrypt($string, 'AES-256-CBC', $key, 0, $initialization_vector);
    $output = base64_encode($output);
    return $output;
}

public static function unlock($string) {
    $keys = parent::join_keys();
    $hash = parent::hash_keys();
    $key = hash('sha256', $keys);
    $initialization_vector = substr(hash('sha256', $hash), 0, 16);
    $output = openssl_decrypt(base64_decode($string), 'AES-256-CBC', $key, 0, $initialization_vector);
    return $output;
}

}

2 Likes

Please provide the GitHub link,

Code is very hard to understand

I will post a like to a demo page soon, this will enplane how to use as well as show how it works.

Link to all files and full code https://encrypt.kvwebsolutions.com/ , hopefully this will help :grinning: