Encryption node Js

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

Hi this is my encryption code i'm trying to decrypt the code in Android via json. I can decrypt this code in node js. But when i tried to decrypt in android error occured so any one suggest me where the problem is occur whether in my node js code or 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);
        })
profilepic.png
manpreet 2 years ago

 

When we are using any kind of Encryption and Decryption into our System (Including DB) our all clients should be having similar credentials to parsing that message.

Lets say we have Backend, Web and Mobile Apps(Android/iPhone). So what backend Encrypt the message with whatever credentials All other client can be having same credentials to decrypt that message.

Here i am suggesting that AES Use 256 with Predefine IV and KEY. And Crypto is very famous library that will having the feature in all the platform. I did with Node.js.

Snippet for Encrypt Text:

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

Snippet for Decrypt Text:

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 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.