<legend id='fpw7l'><style id='fpw7l'><dir id='fpw7l'><q id='fpw7l'></q></dir></style></legend>
  1. <tfoot id='fpw7l'></tfoot>

      <bdo id='fpw7l'></bdo><ul id='fpw7l'></ul>

    1. <small id='fpw7l'></small><noframes id='fpw7l'>

      <i id='fpw7l'><tr id='fpw7l'><dt id='fpw7l'><q id='fpw7l'><span id='fpw7l'><b id='fpw7l'><form id='fpw7l'><ins id='fpw7l'></ins><ul id='fpw7l'></ul><sub id='fpw7l'></sub></form><legend id='fpw7l'></legend><bdo id='fpw7l'><pre id='fpw7l'><center id='fpw7l'></center></pre></bdo></b><th id='fpw7l'></th></span></q></dt></tr></i><div id='fpw7l'><tfoot id='fpw7l'></tfoot><dl id='fpw7l'><fieldset id='fpw7l'></fieldset></dl></div>

      在 PHP openssl 中加密并在 javascript CryptoJS 中解密

      时间:2023-10-15
    2. <small id='O0QYr'></small><noframes id='O0QYr'>

        • <bdo id='O0QYr'></bdo><ul id='O0QYr'></ul>
            <i id='O0QYr'><tr id='O0QYr'><dt id='O0QYr'><q id='O0QYr'><span id='O0QYr'><b id='O0QYr'><form id='O0QYr'><ins id='O0QYr'></ins><ul id='O0QYr'></ul><sub id='O0QYr'></sub></form><legend id='O0QYr'></legend><bdo id='O0QYr'><pre id='O0QYr'><center id='O0QYr'></center></pre></bdo></b><th id='O0QYr'></th></span></q></dt></tr></i><div id='O0QYr'><tfoot id='O0QYr'></tfoot><dl id='O0QYr'><fieldset id='O0QYr'></fieldset></dl></div>

                <tfoot id='O0QYr'></tfoot>
                  <tbody id='O0QYr'></tbody>
                <legend id='O0QYr'><style id='O0QYr'><dir id='O0QYr'><q id='O0QYr'></q></dir></style></legend>
                本文介绍了在 PHP openssl 中加密并在 javascript CryptoJS 中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 PHP 加密一些参数

                I'm encrypting some parameters in PHP using

                 openssl("parameter", "AES-256-ECB", "client")
                

                我希望在 CryptoJS 中解密:

                CryptoJS.AES.decrypt(parameter, "client", {mode: CryptoJS.mode.ECB}).toString(CryptoJS.enc.Utf8);
                

                但它抛出一个空字符串.

                but it's throwing an empty string.

                有什么建议吗?

                推荐答案

                CryptoJS: PHP openssl encrypt -> javascript Describe

                PHP:

                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");
                

                JS:

                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?>'));
                

                CryptoJS:javascript 加密 -> PHP openssl 解密

                JS:

                function CryptoJSAesEncrypt(passphrase, plain_text){
                
                    var salt = CryptoJS.lib.WordArray.random(256);
                    var iv = CryptoJS.lib.WordArray.random(16);
                    //for more random entropy can use : https://github.com/wwwtyro/cryptico/blob/master/random.js instead CryptoJS random() or another js PRNG
                
                    var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999 });
                
                    var encrypted = CryptoJS.AES.encrypt(plain_text, key, {iv: iv});
                
                    var data = {
                        ciphertext : CryptoJS.enc.Base64.stringify(encrypted.ciphertext),
                        salt : CryptoJS.enc.Hex.stringify(salt),
                        iv : CryptoJS.enc.Hex.stringify(iv)    
                    }
                
                    return JSON.stringify(data);
                }
                

                PHP:

                function CryptoJSAesDecrypt($passphrase, $jsonString){
                
                    $jsondata = json_decode($jsonString, true);
                    try {
                        $salt = hex2bin($jsondata["salt"]);
                        $iv  = hex2bin($jsondata["iv"]);          
                    } catch(Exception $e) { return null; }
                
                    $ciphertext = base64_decode($jsondata["ciphertext"]);
                    $iterations = 999; //same as js encrypting 
                
                    $key = hash_pbkdf2("sha512", $passphrase, $salt, $iterations, 64);
                
                    $decrypted= openssl_decrypt($ciphertext , 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iv);
                
                    return $decrypted;
                
                }
                

                这篇关于在 PHP openssl 中加密并在 javascript CryptoJS 中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:用 OpenSSL 替换 Mcrypt 下一篇:致命错误:调用未定义的函数 openssl_random_pseudo_bytes()

                相关文章

                  <tfoot id='5cqQ6'></tfoot><legend id='5cqQ6'><style id='5cqQ6'><dir id='5cqQ6'><q id='5cqQ6'></q></dir></style></legend>

                  <small id='5cqQ6'></small><noframes id='5cqQ6'>

                    <bdo id='5cqQ6'></bdo><ul id='5cqQ6'></ul>

                    <i id='5cqQ6'><tr id='5cqQ6'><dt id='5cqQ6'><q id='5cqQ6'><span id='5cqQ6'><b id='5cqQ6'><form id='5cqQ6'><ins id='5cqQ6'></ins><ul id='5cqQ6'></ul><sub id='5cqQ6'></sub></form><legend id='5cqQ6'></legend><bdo id='5cqQ6'><pre id='5cqQ6'><center id='5cqQ6'></center></pre></bdo></b><th id='5cqQ6'></th></span></q></dt></tr></i><div id='5cqQ6'><tfoot id='5cqQ6'></tfoot><dl id='5cqQ6'><fieldset id='5cqQ6'></fieldset></dl></div>