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 :: php echo sql result 
Php :: check if phone number is valid php 
Php :: creating thumbnail in codeigniter 
Php :: ternary operator for three conditions in php 
Php :: php file extension 
Php :: laravel set config 
Php :: how to receive json data in php 
Php :: laravel blade shorthand if 
Php :: php catch exception 
Php :: laravel pagination keep query string 
Php :: pdo close connection 
Php :: laravel auth 6 
Php :: laravel route target class not found 
Php :: generate slug on create laravel 
Php :: laravel job delay dispatch 
Php :: date time laravel 
Php :: redirect in php 
Php :: running laravel queues in shared hosting 
Php :: Target class [BannerController] does not exist. 
Php :: laravel eloquent orderby 
Php :: bycrypt password php 
Php :: how to convert unix timestamp to date php 
Php :: file_put_contents 
Php :: laravel add user 
Php :: laravel api response json 
Php :: laravel create request 
Php :: php check if query succeeded 
Php :: symfony set timezone 
Php :: use font awesome in laravel 8 
Php :: redirect back laravel 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =