Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Encrypt in PHP openssl and decrypt in javascript CryptoJS

function CryptoJSAesDecrypt(passphrase,encrypted_json_string){

    var obj_json = JSON.parse(encrypted_json_string);

    var encrypted = obj_json.ciphertext;
    var salt = CryptoJS.enc.Hex.parse(obj_json.salt);
    var iv = CryptoJS.enc.Hex.parse(obj_json.iv);   

    var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999});


    var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv});

    return decrypted.toString(CryptoJS.enc.Utf8);
}

console.log(CryptoJSAesDecrypt('your passphrase','<?php echo $string_json_fromPHP?>'));
Comment

Encrypt in PHP openssl and decrypt in javascript CryptoJS

function CryptoJSAesEncrypt($passphrase, $plain_text){

    $salt = openssl_random_pseudo_bytes(256);
    $iv = openssl_random_pseudo_bytes(16);
    //on PHP7 can use random_bytes() istead openssl_random_pseudo_bytes()
    //or PHP5x see : https://github.com/paragonie/random_compat

    $iterations = 999;  
    $key = hash_pbkdf2("sha512", $passphrase, $salt, $iterations, 64);

    $encrypted_data = openssl_encrypt($plain_text, 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iv);

    $data = array("ciphertext" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "salt" => bin2hex($salt));
    return json_encode($data);
}

$string_json_fromPHP = CryptoJSAesEncrypt("your passphrase", "your plain text");
Comment

PREVIOUS NEXT
Code Example
Php :: Laravel - Query Builder Left join 
Php :: Tenant could not be identified on domain tenancy 
Php :: laravel route optional parameter 
Php :: sass mix laravel 
Php :: php exec get pid 
Php :: run laravel cron job on cpanel 
Php :: ziparchive php example 
Php :: isempty php 
Php :: sql update row in php 
Php :: php check image size before upload 
Php :: laravel trans with parameters 
Php :: import faker in laravel 
Php :: print in file php 
Php :: laravel number input positive only 
Php :: laravel search multiple tables 
Php :: laravel get view variables 
Php :: laravel has many 
Php :: drop table phpmyadmin 
Php :: Use DateTime() and DateInterval() Objects for PHP 5.3 and Above and Calculate the Difference Between Two Dates Using PHP 
Php :: convert png image transparent into webp php 
Php :: laravel where 2 column 
Php :: mysql Cannot pass parameter 2 by reference 
Php :: laravel factory in custom namespace 
Php :: laravel make factory 
Php :: explode return empty array 
Php :: array marge in php 
Php :: check current user role 
Php :: php pdo example 
Php :: php insert to mysql 
Php :: last insert id mysqli 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =