Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加密AES解密在节点js API中不起作用_Javascript_Node.js_Reactjs_Encryption_Cryptojs - Fatal编程技术网

Javascript 加密AES解密在节点js API中不起作用

Javascript 加密AES解密在节点js API中不起作用,javascript,node.js,reactjs,encryption,cryptojs,Javascript,Node.js,Reactjs,Encryption,Cryptojs,在我的网站上,我在react前端使用加密AES加密对对象进行了加密。 现在我必须在NodeJS后端解密它 我在两边使用相同的密钥、相同版本的加密JS包、相同的节点版本。 但是react前端的加密字符串在节点JS后端没有被解密 我使用了下面的代码进行加密和解密 前端加密 const pinObj = { pin: this.state.pin_number, }; let ciphertext = crypto.AES.encrypt(JSON.stringify(pinObj), con

在我的网站上,我在react前端使用加密AES加密对对象进行了加密。 现在我必须在NodeJS后端解密它

我在两边使用相同的密钥、相同版本的加密JS包、相同的节点版本。 但是react前端的加密字符串在节点JS后端没有被解密

我使用了下面的代码进行加密和解密

前端加密

const pinObj = {
    pin: this.state.pin_number,
};
let ciphertext = crypto.AES.encrypt(JSON.stringify(pinObj), config.aesSecretkey);
const encryptedText = ciphertext.toString();
const encryptedText = data['encrypted_pin_number'];
const bytes = crypto.AES.decrypt(encryptedText, config.aesSecretkey);
const decryptedData = bytes.toString(crypto.enc.Utf8);
const pinObj = JSON.parse(decryptedData);
在节点JS后端进行解密

const pinObj = {
    pin: this.state.pin_number,
};
let ciphertext = crypto.AES.encrypt(JSON.stringify(pinObj), config.aesSecretkey);
const encryptedText = ciphertext.toString();
const encryptedText = data['encrypted_pin_number'];
const bytes = crypto.AES.decrypt(encryptedText, config.aesSecretkey);
const decryptedData = bytes.toString(crypto.enc.Utf8);
const pinObj = JSON.parse(decryptedData);
我在
decryptedData
变量中得到了空字符串


您能提出问题所在和解决方案吗?

代码非常简单,似乎不包含bug。如果两个代码在同一环境中执行,则解密工作正常。您应该检查解密端和加密端的
encryptedText
是否相同。问题可能出在数据传输中。此外,还应检查键的相等性。最好发布示例数据(明文、密码和密文)。看起来您正在尝试加密客户端和服务器之间的传输通道。但是https会自动为您执行此操作。曾经有人试图发明这种加密技术,但现在你可以免费保护服务器和客户端之间的通信。您真的需要在已经加密的通信通道中加密某些内容吗?@Topaco是的,我已经检查过,
encryptedText
在两侧都是相同的,并且在两侧使用相同的密钥。@MaximSagaydachny是的,我正在传递用户的PIN码以访问web门户。因此,我的客户不希望以简单的形式传递它。正如我所说,在相同的环境中,两种代码都可以工作。您应该发布React前端代码的示例,即
JSON.stringify(pinObj)
config.aesSecretKey
ciphertext.toString()的值。