gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:17:41 24 4
gpt4 key购买 nike

我一直在尝试找出如何做到这一点:

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

和 Node :

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

关于php - 将 PHP 加密/解密转换为 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com