如何使用crypto模块(服务器端)和crypto-js(客户端,react-native)正确加密/解密node.js之间的数据?
注意:我在 react-native 项目中使用了 cryptojs,因此我不能在客户端上使用加密。更换加密服务器端对我来说不是一个选择。
服务端代码:
var Crypto = require("crypto");
var Cipher = {
pass: "0123456789abcdef0123456789abcdef",
iv: "0123456789abcdef",
encript: function (msg) {
try {
var cipher = Crypto.createCipheriv("aes-256-cbc", this.pass, this.iv);
var hash = cipher.update(msg, 'utf8', "hex");
var hex = hash + cipher.final("hex");
return hex;
} catch (err) {
console.error(err);
return "";
}
},
decript: function (hex){
try {
var decipher = Crypto.createDecipheriv("aes-256-cbc", this.pass, this.iv);
var dec = decipher.update(hex, "hex", 'utf8');
return dec + decipher.final('utf8');
} catch (err) {
console.error(err);
return …Run Code Online (Sandbox Code Playgroud)