2016-07-07 73 views
3

嗨,这是我的加密代码,我试图通过json解密Android中的代码。我可以在节点js中解密这段代码。但是,当我试图解密在android错误发生,所以任何人建议我在哪里出现问题,无论是在我的节点js代码或android。加密节点Js

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

回答

0

当我们在使用任何类型的加密和解密的进入我们的系统(包括DB)我们所有的客户应当具有类似凭据来解析该消息。

可以说我们有后端,网络和移动应用程序(Android/iPhone)。那么后端是用什么凭证加密消息所有其他客户端可以拥有相同的凭证来解密该消息。

这里我建议AES使用256与Predefine IV和KEY。并且加密是将在所有平台中具有特性的非常有名的库。我使用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; 
    }, 

代码段用于解密文本:

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

可以incrypt使用盐和哈希 第一生成盐

var salt = crypto.randomBytes(16).toString('base64'); 

秒生成加密密码

var password = crypto.pbkdf2Sync(password, new Buffer(this.salt, 'base64'), 10000, 64, 'sha1').toString('base64'); 

使用相同的方法执行登录。