Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express'),
- app = express(),
- cors = require('cors'),
- helmet = require('helmet'),
- mysql = require('mysql'),
- bodyParser = require('body-parser'),
- crypto = require('crypto'),
- algorithm = 'aes-256-ctr',
- password = 'zyxcba',
- port = 3000;
- app.use(cors());
- app.use(helmet());
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({
- extended: false
- }));
- var database = mysql.createPool({
- connectionLimit : 100,
- host: 'localhost',
- user: '*****',
- password: '*****',
- database: 'devesh',
- port: 3306
- });
- /**
- * Encrypt function
- */
- function encrypt(text){
- var cipher = crypto.createCipher(algorithm,password)
- var crypted = cipher.update(text,'utf8','hex')
- crypted += cipher.final('hex');
- return crypted;
- }
- /**
- * Decrypt function
- */
- function decrypt(text){
- var decipher = crypto.createDecipher(algorithm,password)
- var dec = decipher.update(text,'hex','utf8')
- dec += decipher.final('utf8');
- return dec;
- }
- app.post('/encrypt',function(req,res){
- const appData = {};
- const first_name = req.body.first_name;
- const last_name = req.body.last_name;
- const age = req.body.age;
- const userData = {
- first_name: encrypt(first_name),
- last_name : encrypt(last_name),
- age: age
- }
- database.getConnection(function(err,connection){
- if(err){
- appData['error'] = 1;
- appData['data'] = 'Internal server error'
- res.status(500).json(appData);
- }else{
- connection.query('INSERT INTO employee SET?',userData,function(err,rows,fields){
- if(err){
- appData['error'] = 1;
- appData['data'] = 'Error adding data'
- res.status(500).json(appData);
- }else{
- appData['error'] = 0;
- appData['data'] = 'Data saved in encrypted form succesfully!'
- res.status(200).json(appData);
- }
- });
- connection.release();
- }
- });
- });
- app.listen(port, function(){
- console.log('Server listening to port',port);
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement