加密数据

Posted yangshifu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加密数据相关的知识,希望对你有一定的参考价值。

const si = require(‘systeminformation‘);
const express = require(‘express‘);
const bodyParser = require(‘body-parser‘);
const crypto = require(‘crypto‘);
const uuidv4 = require(‘uuid/v4‘);
const detect = require(‘detect-port‘);
const exps = express();
const router = express.Router();
const cryptoAlgorithm = ‘aes-256-ctr‘;
const uuidPath = path.join(__dirname + ‘uuid.txt‘);


function encrypt(text,cryptoPW){
    try{
        var cipher = crypto.createCipher(cryptoAlgorithm,cryptoPW);
        var crypted = cipher.update(text,‘utf8‘,‘hex‘);
        crypted += cipher.final(‘hex‘);
        return crypted;
    }
    catch(err){
        return "";
    }
}

function decrypt(text,cryptoPW){
    try{
        var decipher = crypto.createDecipher(cryptoAlgorithm,cryptoPW);
        var dec = decipher.update(text,‘hex‘,‘utf8‘);
        dec += decipher.final(‘utf8‘);
        return dec;
    }
    catch(err){
        return "";
    }
}

router.use(function(req, res, next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization,merchant-id,pos-merchant-id");
    next(); //make sure we go to the next routes and dont stop here
});

router.route(‘/saveSecretKey‘).post(function(req, res){
    var merchantId = req.body.merchantId ? req.body.merchantId : "trialtest";
    var secretkey = req.body.secret;
    var appInstanceType = req.body.appType;
    si.networkInterfaces(function(data){
        var encryptPW =  data[0].mac + ‘-‘ + merchantId;
        var localSecretKey = encrypt(secretkey,encryptPW);
        fs.readFile(uuidPath,function(readErr,fileData){
            if(readErr || fileData.toString() == ""){
                fs.open(uuidPath,‘w+‘,function(openErr, fd){
                    if(openErr){
                        var resData = {
                            successful: false,
                            failurereason:"License secret key is lost"
                        }
                        res.json(resData);
                    }
                    else{
                        var secretInfo = [{‘instanceType‘:appInstanceType,‘secretkey‘:localSecretKey}];
                        var secretInfoStr = JSON.stringify(secretInfo);
                        fs.writeFile(fd,secretInfoStr,function(writeErr){
                            if(writeErr) {
                                var resData = {
                                    successful: false,
                                    failurereason:"License secret key is invalid"
                                }
                                res.json(resData);
                            }
                            fs.close(fd,function(){
                                var resData = {
                                    successful:true
                                };
                                res.json(resData);
                                console.log(‘uuid writen‘);
                            })
                        })
                    }
                })
            }
            else{
                fs.open(uuidPath,‘w+‘,function(openErr, fd){
                    if(openErr){
                        var resData = {
                            successful: false,
                            failurereason:"Can not open license secret key file"
                        }
                        res.json(resData);
                    }
                    else{
                        var secretKeyJson = JSON.parse(fileData.toString());
                        var isExisted = false;
                        for(var i in secretKeyJson){
                            if(secretKeyJson[i].instanceType == appInstanceType){
                                secretKeyJson[i].secretkey = localSecretKey;
                                isExisted = true;
                                break;
                            }
                        }
                        if(isExisted == false){
                            var secretInfo = {‘instanceType‘:appInstanceType,‘secretkey‘:localSecretKey};
                            secretKeyJson.push(secretInfo);
                        }
                        var secretInfoStr = JSON.stringify(secretKeyJson);
                        fs.writeFile(fd,secretInfoStr,function(writeErr){
                            if(writeErr) {
                                var resData = {
                                    successful: false,
                                    failurereason:"License secret key is invalid"
                                }
                                res.json(resData);
                            }
                            fs.close(fd,function(){
                                var resData = {
                                    successful:true
                                };
                                res.json(resData);
                                console.log(‘uuid writen‘);
                            })
                        })
                    }
                })
            }
        })
    });
});

router.route(‘/getSecretKey‘).post(function(req,res){
    console.log("************get key*****************************")
    var merchantId = req.body.merchantId ? req.body.merchantId : "trialtest";
    var appInstanceType = req.body.appType;
    si.networkInterfaces(function(sysdata){
        var encryptPW = sysdata[0].mac + ‘-‘ + merchantId;
        fs.readFile(uuidPath,function(err,data){
            if(err || data.toString() == ""){
                var resData = {
                    successful:true,
                    secret:""
                }
                console.log("readErorr");
                res.json(resData);
            }
            else{
                var secretkeyJson = JSON.parse(data);
                var theSecretKey = "";
                for(var i in secretkeyJson){
                    if(secretkeyJson[i].instanceType == appInstanceType){
                        theSecretKey = secretkeyJson[i].secretkey;
                        break;
                    }
                }
                var secretkeyStr = decrypt(theSecretKey.toString(),encryptPW);
                var resData = {
                    successful:true,
                    secret:secretkeyStr
                }
                res.json(resData);
            }
        })
    });
});

  

以上是关于加密数据的主要内容,如果未能解决你的问题,请参考以下文章

论如何设计一款端对端加密通讯软件

Laravel基本使用

使用ffmpeg视频切片并加密

WPYOU主题加密码代码的解码

(转)博客园登陆__JSEncrypt 分析

超级有用的9个PHP代码片段