【问题标题】:Porting Decryption Function From PHP to NodeJs将解密函数从 PHP 移植到 NodeJs
【发布时间】:2021-11-09 06:41:18
【问题描述】:

将函数从 PHP 移植到 NodeJS 时遇到一些问题。我试过用 Node JS 实现这个 PHP 代码,但它不起作用。

这是PHP中的代码

   <?php 
        require_once 'vendor/autoload.php';
    
        // function decrypt
        function stringDecrypt($key, $string){
            
      
            $encrypt_method = 'AES-256-CBC';
    
            // hash
            $key_hash = hex2bin(hash('sha256', $key));
      
            // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
            $iv = substr(hex2bin(hash('sha256', $key)), 0, 16);
    
            $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key_hash, OPENSSL_RAW_DATA, $iv);
      
            return $output;
        }
        
    ?>

这是我在 NodeJs 中的代码

    function decryptResponse(timestamp, string, key) {
        var key_hash = hex2bin(crypto.createHash("sha256").update(key).digest('hex'));
        var iv = key_hash.substr(0,16);
        var decoder = crypto.createDecipheriv('aes-256-cbc', key_hash, iv);
        var output = decoder.update(Buffer.from(string).toString('base64'),'base64','utf8') += decoder.final('utf8');
        console.log("Decrypt Result : ", output); //Not Showing on Log
    }

function hex2bin(hex) {
    var bytes = [];
    var str;
    for(var i=0; i< hex.length-1; i+=2){
        bytes.push(parseInt(hex.substr(i, 2), 16));
    }
    str = String.fromCharCode.apply(String, bytes);

    return str;
  }

当我从 API 获得响应并需要将其发送给用户时调用此函数。

var decompressedResponse = decryptResponse(timestamp, response.data.response, key);  
res.send(decompressedResponse);

我需要这个函数来解密来自 API 的响应,所以我真的需要这个函数。感谢您的帮助。

【问题讨论】:

  • 在 PHP 代码中没有使用 decompress() 函数,为什么要发布它(以及为什么在 NodeJS 代码中应用它)? NodeJS代码中的hex2bin()也没有定义。
  • @Topaco 抱歉,我将在 nodejs 上添加 hex2bin(),对于 PHP,我不太了解它,因为它不是我的。基本的是,你需要用aes-256-cbc解密API响应,然后用LZString解压解密结果
  • 我建议在单独的问题中询问解压缩问题(如果需要的话)。目前,您在这方面的信息不一致,这使得很难回答整个问题:在 PHP 代码中 decompress() 根本没有被调用,并且在 NodeJS 代码中缺少实现。
  • @Topaco 好吧,我将删除它,因为主要问题是解密。谢谢你的建议
  • @Topaco 谢谢你的建议。删除 hex2bin 函数并将密钥哈希作为 bffer 返回,现在它可以工作了。你能用它作为答案吗?我会接受它作为正确答案。谢谢。

标签: php node.js cryptojs


【解决方案1】:

hex2bin() 函数不需要,可以删除。

另外,将 key 和 IV 确定为缓冲区更容易。

密文目前在update() 中进行了第二次Base64 编码。为避免这种情况,应将其直接传递给update()

并且update()final() 调用结果的连接必须使用+ 而不是+= 完成(这可能只是一个拼写错误或复制/粘贴错误)。

总体:

function decryptResponse(timestamp, string, key) {
    var key_hash = crypto.createHash("sha256").update(key).digest(); // remove hex2bin; use Buffer
    var iv = key_hash.slice(0,16); // use Buffer
    var decoder = crypto.createDecipheriv('aes-256-cbc', key_hash, iv);
    var output = decoder.update(string,'base64','utf8') + decoder.final('utf8'); // don't Base64 encode twice, pass ciphertext directly; apply + instead of +=
    console.log("Decrypt Result : ", output); 
}

请注意,将密钥或密钥的一部分用作 IV 是不安全的。通常,(非秘密)IV 是为每次加密随机生成的,并与密文(通常连接)一起传递。

此外,使用散列作为密钥派生函数是不安全的。相反,应该应用可靠的密钥派生函数,例如 PBKDF2。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多