加密节点Js

cho*_*han 5 android node.js

您好,这是我的加密代码,我正在尝试通过 json 解密 Android 中的代码。我可以在 Node js 中解密这段代码。但是当我尝试在 android 中解密时,发生了错误,所以有人建议我问题发生在哪里,无论是在我的 Node js 代码中还是在 android 中。

app.post('/insert',  function (req,res){
        var data = {
            userId:req.body.fname,
            firstName:req.body.fname
        };


                var cipher = crypto.createCipher('aes128', 'a password');
                 data.firstName = cipher.update(data.firstName, 'utf8','base64' );
                data.firstName += cipher.final('base64');
                console.log(data);

         con.query("insert into md5 set ?",[data], function (err,rows){
            if(err) throw err;  
                res.send("Value has been inserted");
            })
             console.log(data.firstName);
        })
Run Code Online (Sandbox Code Playgroud)

Sit*_*ten 0

当我们在系统(包括数据库)中使用任何类型的加密和解密时,我们的所有客户端都应该具有类似的凭据来解析该消息。

假设我们有后端、网络和移动应用程序(Android/iPhone)。那么后端使用任何凭据加密消息所有其他客户端都可以使用相同的凭据来解密该消息。

在这里我建议 AES 使用256 和 Predefine IV 和 KEY。Crypto非常著名的库,在所有平台上都具有该功能。我是用 Node.js 做的。

加密文本的片段:

encryptText: function(text) {
        var cipher = crypto.createCipheriv(Constant.algo, Constant.key, Constant.iv);
        var result = cipher.update(text, "utf8", 'base64');
        result += cipher.final('base64');
        return result;
    },
Run Code Online (Sandbox Code Playgroud)

解密文本片段:

decryptText: function(text) {
        console.log(text);

        var decipher = crypto.createDecipheriv(Constant.algo, Constant.key, Constant.iv);
        var result = decipher.update(text, 'base64');
        result += decipher.final();
        return result;
    },
Run Code Online (Sandbox Code Playgroud)