我如何在由php函数加密的node.js中集成解密函数

时间:2016-05-21 10:09:40

标签: javascript php node.js encryption

这是我用于加密和解密的我的PHP代码。这个方法也在使用exec-php节点模块在node.js上工作但现在我想在节点和i&中创建类似的功能#39; m使用加密节点模块,但我无法正确解密数据。

function encrypt_decrypt($action, $string) {
    $output = false;
    $encrypt_method = "AES-256-CBC";
    $secret_key = 'Key for encryption and decryption';
    $secret_iv = 'IV for openssl';

    // 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;
}

0 个答案:

没有答案