Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php aes

function encrypt($plaintext, $password) {
    $method = "AES-256-CBC";
    $key = hash('sha256', $password, true);
    $iv = openssl_random_pseudo_bytes(16);

    $ciphertext = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
    $hash = hash_hmac('sha256', $ciphertext . $iv, $key, true);

    return base64_encode($iv . $hash . $ciphertext);
}

function decrypt($encryptedText, $password) {
    $encryptedText = base64_decode($encryptedText);

    $method = "AES-256-CBC";
    $iv = substr($encryptedText, 0, 16);
    $hash = substr($encryptedText, 16, 32);
    $ciphertext = substr($encryptedText, 48);
    $key = hash('sha256', $password, true);

    if (!hash_equals(hash_hmac('sha256', $ciphertext . $iv, $key, true), $hash)) return null;

    return openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv);
}
Comment

PREVIOUS NEXT
Code Example
Php :: bind to class blade laravel 
Php :: inplode php 
Php :: wordpress query get results 
Php :: comment split une chaine de caratere en php 
Php :: php localhost 
Php :: acosh php 
Php :: laravel where and where 
Php :: php array push 
Php :: database, counts,php, 
Php :: laravel updateorcreate multiple records 
Php :: PHP $argv echo 
Php :: use session in laravel 
Php :: php echo html and variable 
Php :: laravel resource api 
Php :: create new record via model in laravel 
Php :: how to convert enum to string in php 
Php :: how to loop by index in php 
Php :: newline not working php 
Php :: laravel migration table softdeletes 
Php :: laravel filter array 
Php :: download file using jquery php 
Php :: how to access array using key in php 
Php :: laravel migration mediumtext length 
Php :: year dropdown loop in php 
Php :: no routes.php in http folder 
Php :: rodar migration laravel 
Php :: laravel swagger 
Php :: laravel query relationship nested 
Php :: laravel firstorcreate usage 
Php :: laravel find model inside the same model 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =