繁体   English   中英

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

[英]Converting PHP encrypt/decrypt to Node.js

我一直在尝试找出方法:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM