note 121982 added to function.openssl-encrypt
| From: | omidbahrami1990 at gmail dot com | Date: | Tue, 05 Dec 2017 23:35:23 +0000 |
| Subject: | note 121982 added to function.openssl-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209317@lists.php.net to get a copy of this message | ||
This Is The Most Secure Way To Encrypt And Decrypt Your Data,
It Is Almost Impossible To Crack Your Encryption.
--------------------------------------------------------
--- Create Two Random Keys And Save Them In Your Configuration File ---
<?php
// Create The First Key
echo base64_encode(openssl_random_pseudo_bytes(32));
// Create The Second Key
echo base64_encode(openssl_random_pseudo_bytes(64));
?>
--------------------------------------------------------
<?php
// Save The Keys In Your Configuration File
define('FIRSTKEY','Lk5Uz3slx3BrAghS1aaW5AYgWZRV0tIX5eI0yPchFz4=');
define('SECONDKEY','EZ44mFi3TlAey1b2w4Y7lVDuqO+SRxGXsa7nctnr/JmMrA2vN6EJhrvdVZbxaQs5jpSe34X3ejFK/o9+Y5c83w==');
?>
--------------------------------------------------------
<?php
function secured_encrypt($data)
{
$first_key = base64_decode(FIRSTKEY);
$second_key = base64_decode(SECONDKEY);
$method = "aes-256-cbc";
$iv_length = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($iv_length);
$first_encrypted = openssl_encrypt($data,$method,$first_key, OPENSSL_RAW_DATA ,$iv);
$second_encrypted = hash_hmac('sha3-512', $first_encrypted, $second_key, TRUE);
$output = base64_encode($iv.$second_encrypted.$first_encrypted);
return $output;
}
?>
--------------------------------------------------------
<?php
function secured_decrypt($input)
{
$first_key = base64_decode(FIRSTKEY);
$second_key = base64_decode(SECONDKEY);
$mix = base64_decode($input);
$method = "aes-256-cbc";
$iv_length = openssl_cipher_iv_length($method);
$iv = substr($mix,0,$iv_length);
$second_encrypted = substr($mix,$iv_length,64);
$first_encrypted = substr($mix,$iv_length+64);
$data = openssl_decrypt($first_encrypted,$method,$first_key,OPENSSL_RAW_DATA,$iv);
$second_encrypted_new = hash_hmac('sha3-512', $first_encrypted, $second_key, TRUE);
if (hash_equals($second_encrypted,$second_encrypted_new))
return $data;
return false;
}
?>
----
Server IP: 208.43.231.9
Probable Submitter: 128.199.193.75
----
Manual Page -- http://php.net/manual/en/function.openssl-encrypt.php
Edit -- https://master.php.net/note/edit/121982
Del: integrated -- https://master.php.net/note/delete/121982/integrated
Del: useless -- https://master.php.net/note/delete/121982/useless
Del: bad code -- https://master.php.net/note/delete/121982/bad+code
Del: spam -- https://master.php.net/note/delete/121982/spam
Del: non-english -- https://master.php.net/note/delete/121982/non-english
Del: in docs -- https://master.php.net/note/delete/121982/in+docs
Del: other reasons-- https://master.php.net/note/delete/121982
Reject -- https://master.php.net/note/reject/121982
Search -- https://master.php.net/manage/user-notes.php