将PHP加密/解密转换为Node.js

时间:2019-01-12 03:34:57

标签: php node.js encryption

我一直在尝试找出方法:

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

1 个答案:

答案 0 :(得分:3)

主要问题是一个令人尴尬的问题。我们主要是这个项目的两个开发人员,我认为我正在编辑的用于加密和解密的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;
}