android - 加密 Node Js

标签 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);
        })

最佳答案

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

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

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

加密文本的 fragment :

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

解密文本的 fragment :

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

关于android - 加密 Node Js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38240419/

相关文章:

android - xml获取宽度并设置为android :height

node.js - node-http-proxy 如何将 cookie 转发到代理目标

ruby-on-rails - 我如何在 ReactJS 中使用嵌套表单?

node.js - Sequelize.js 如何从 MySQL 映射 varbinary 类型

android - 解决依赖关系时出错

android - 如何使用 Android Room Persistence Library 实现多对多关系?

Android Shape Drawable改变属性

android - Android Sqlite 字符串的最大长度?

javascript - 在浏览器中解压来自 Node.js 的 JSON 数据文件作为静态文件

node.js - 使用 OrientDB 内存数据库作为 session 处理程序而不是 REDIS 是个好主意吗