【问题标题】:Converting PHP encrypt/decrypt to Node.js将 PHP 加密/解密转换为 Node.js
【发布时间】:2019-06-06 23:16:01
【问题描述】:

我一直在尝试找出如何制作这个:

function encrypt_decrypt($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = 'HqFdkh2FX126fH1r';
    $secret_iv = 'iS2dk82dXd26f61K';

    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
        $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if ( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    } else if( $action == 'decrypt' ) {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

在 Node.js 中

原因是加密由PHP处理,解密由Node处理。

编辑:

我设法做到了这一点:

var crypto = require('crypto')
            , key = 'cI8Jd96NDoasd09jcI8Jd96NDoasd09j'
            , iv = 'cI8Jd96NDoasd09j'
            , plaintext = '2';

            hashedKey = crypto.createHash('sha256').update(key, 'utf-8').digest('hex');
            console.log('hashed key=', hashedKey);
            // corresponds to the hashed key in PHP

            hashedIv = crypto.createHash('sha256').update(iv, 'utf-8').digest('hex').substring(0,16);
            console.log('hashed iv=', hashedIv);
            // corresponds to the hashed iv in PHP

            var buf = Buffer.from(teamId, 'base64');
            console.log("buffer: " + buf);

而变量buf实际上和PHP代码中的base64_decode($string是一样的。

但是,当我这样做时:

    var decipher = crypto.createDecipheriv("aes-256-cbc",key, iv);
    var decrypted = decipher.update(buf,  'base64', 'utf8');
    console.log("decrypted.toString(): " + decrypted.toString());

我在控制台中收到Z���ߋd�M:��,而不是所需的2

【问题讨论】:

  • IV 必须相同且密钥
  • 他们是。我一直在尝试使用散列键/iv 和未散列来执行此操作,但问题仍然存在。
  • 让我纠正一下,php中的密钥是HqFdkh2FX126fH1r和JS cI8Jd96NDoasd09jcI8Jd96NDoasd09j,解密部分在哪里?你看到了吗this
  • 我明白了。我尝试放置相同的密钥,但在对其进行哈希处理后(就像我在 PHP 中所做的那样)节点抱怨密钥和 iv 字符太多。
  • 那是因为您对摘要进行了十六进制编码,因此您的密钥是 64 个字符的字符串。 PHP 的hash() 默认也对哈希进行十六进制编码;如果 $raw_output 参数设置为 true,它将返回原始字节。

标签: php node.js encryption


【解决方案1】:

主要问题是一个令人尴尬的问题。我们主要是这个项目的两个开发人员,我认为我正在编辑的用于加密和解密的 php 文件是我唯一需要关心的事情。

后来发现实际的编码调用来自另一个 php 文件。因此,我对正在处理的文件中的编码所做的更改都是徒劳的。

对于任何感兴趣的人来说,最终结果如下所示:

    function encrypt_decrypt($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = '32 byte key';
    $secret_iv = '16 byte iv';

    // hash
    $key = substr(hash('sha256', $secret_key), 0, 32);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
        $iv = substr(hash('sha256', $secret_iv), 0, 16);

        if ( $action == 'encrypt' ) {
                $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
                //$output = base64_encode($output);
        } else if( $action == 'decrypt' ) { // this below is now handled in Node
                $output = openssl_decrypt($string, $encrypt_method, $key, 0, $iv);
                //$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
        }
    return $output;
}

和节点:

function getDecryptedTeamId(encryptedId) {
    var hashedKey;
    var hashedIv;
    var crypto = require('crypto')
    , key = 'same 32 byte key as above in php'
    , iv = 'same 16 byte ivas above in php'
    , plaintext = '2';

    hashedKey = crypto.createHash('sha256').update(key, 'utf-8').digest('hex').substring(0,32);
    key = hashedKey;

    hashedIv = crypto.createHash('sha256').update(iv, 'utf-8').digest('hex').substring(0,16);
    iv = hashedIv;

    var buf = Buffer.from(encryptedId, 'base64');
    var crypt = buf.toString('base64');

    var decryptor = crypto.createDecipheriv("aes-256-cbc", hashedKey, hashedIv);
    var teamIdDec = decryptor.update(buf);
    teamIdDec += decryptor.final();
    return teamIdDec;
}

【讨论】:

  • 我对 AWS KMS 的使用并不满意,它适用于某些设备,但它决定不适用于其他设备。到目前为止,这一直按预期工作 - 谢谢!
猜你喜欢
  • 2014-04-21
  • 1970-01-01
  • 2014-11-03
  • 2018-08-16
  • 2011-02-26
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
相关资源
最近更新 更多