Search
 
SCRIPT & CODE EXAMPLE
 

PHP

openssl encrypt php with key

<?php
//$key previously generated safely, ie: openssl_random_pseudo_bytes
$plaintext = "message to be encrypted";
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
$ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw );

//decrypt later....
$c = base64_decode($ciphertext);
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len=32);
$ciphertext_raw = substr($c, $ivlen+$sha2len);
$original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
if (hash_equals($hmac, $calcmac))// timing attack safe comparison
{
    echo $original_plaintext."
";
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: how to set time ago in php 
Php :: error_log wordpress 
Php :: Only variables should be passed by reference in 
Php :: laravel array cast 
Php :: Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1 
Php :: type hidden value put laravel 
Php :: how to remove array index from json in php 
Php :: woocommerce order item get product id 
Php :: php how to connect to db using PDO 
Php :: check if file empty php 
Php :: install php apache 
Php :: php use function from same class 
Php :: convert std to array php 
Php :: ver version de php en linux 
Php :: Clear and delete the folder after the time specified in php 
Php :: yesterday php 
Php :: php filter array 
Php :: 301 redirect 
Php :: php sort array by value 
Php :: macos how host laravel website on localhost and intranet wifi 
Php :: model find by certain column laravel 
Php :: php-fpm docker 
Php :: autoload_namespaces.php failed to open stream: Permission denied 
Php :: put woocommerce orders on pending payment automatically 
Php :: add custom post type wordpress 
Php :: laravel run all seeders 
Php :: php read from mariadb 
Php :: laravel select raw where 
Php :: remove last comma from string php foreach 
Php :: laravel move file from local to s3 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =