加密:接口加密解密
This commit is contained in:
@@ -1,34 +1,6 @@
|
||||
import { ENCRYPT_KEY, ENCRYPT_IV } from '@consts';
|
||||
import { genCode } from 'app/pubUtils/util';
|
||||
import { MsgEncrypt } from "app/pubUtils/sysUtil";
|
||||
import { Context } from 'egg';
|
||||
const crypto = require('crypto');
|
||||
const isJSON = require('koa-is-json');
|
||||
import fs = require('fs');
|
||||
import path = require('path');
|
||||
|
||||
const privateKey = fs.readFileSync(path.resolve(__dirname, `../resource/privateKey`));
|
||||
|
||||
function aesEncrypt(data, key, iv) {
|
||||
const cipher = crypto.createCipheriv('aes-192-cbc', key, iv);
|
||||
let crypted = cipher.update(data, 'utf8', 'hex');
|
||||
crypted += cipher.final('hex');
|
||||
return crypted;
|
||||
}
|
||||
|
||||
function aesDecrypt(data, key, iv) {
|
||||
const decipher = crypto.createDecipheriv('aes-192-cbc', key, iv);
|
||||
let decrypted = decipher.update(data, 'hex', 'utf8');
|
||||
decrypted += decipher.final('utf8');
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
function privateDecrypt(encryptMsg) {
|
||||
const decryptMsg = crypto.privateDecrypt(
|
||||
{ key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING },
|
||||
encryptMsg
|
||||
);
|
||||
return decryptMsg;
|
||||
}
|
||||
|
||||
module.exports = options => {
|
||||
return async function parmsDecode(ctx: Context, next) {
|
||||
@@ -42,16 +14,7 @@ module.exports = options => {
|
||||
}
|
||||
if (options.threshold && ctx.length < options.threshold) return;
|
||||
const reqBody = ctx.request.body;
|
||||
|
||||
|
||||
const reqHeader = ctx.request.header;
|
||||
const aesKey = reqHeader['k'] ? privateDecrypt(new Buffer(reqHeader['k'], 'base64')) : ENCRYPT_KEY;
|
||||
const aesIV = reqHeader['v'] ? privateDecrypt(new Buffer(reqHeader['v'], 'base64')) : ENCRYPT_IV;
|
||||
|
||||
if (isJSON(reqBody)) {
|
||||
const encodeStr = aesEncrypt(JSON.stringify(reqBody), aesKey, aesIV);
|
||||
console.log(`encoded str: ${encodeStr}`);
|
||||
}
|
||||
|
||||
console.log(ctx.app.config.decodeParm)
|
||||
if(ctx.app.config.decodeParm == false) {
|
||||
@@ -59,13 +22,18 @@ module.exports = options => {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!reqBody.data) return;
|
||||
|
||||
const decodeStr = aesDecrypt(reqBody.data, aesKey, aesIV);
|
||||
ctx.logger.debug('decoded str:', decodeStr);
|
||||
let msgEncrypt = new MsgEncrypt({ encodeK: reqHeader['k'], encodeV: reqHeader['v'] });
|
||||
const { aesKey, aesIV } = msgEncrypt.getKv();
|
||||
|
||||
console.log(`encode str ${msgEncrypt.encryptMsg(reqBody)}`);
|
||||
|
||||
try {
|
||||
ctx.request.body = JSON.parse(decodeStr);
|
||||
let decryptResult = msgEncrypt.decryptMsg(reqBody.data);
|
||||
if(!decryptResult) throw new Error('params parse err');
|
||||
|
||||
ctx.request.body = decryptResult;
|
||||
console.log('req body', ctx.request.body);
|
||||
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] request: ${JSON.stringify(ctx.request.body)}`)
|
||||
} catch (e) {
|
||||
@@ -82,11 +50,12 @@ module.exports = options => {
|
||||
}
|
||||
const resBody = ctx.body;
|
||||
console.log('return value:', JSON.stringify(resBody));
|
||||
if (isJSON(resBody)) {
|
||||
ctx.body = { result: aesEncrypt(JSON.stringify(resBody), aesKey, aesIV) };
|
||||
let resBodyWithEncrypt = msgEncrypt.encryptMsg(resBody);
|
||||
if (resBodyWithEncrypt) {
|
||||
ctx.body = { result: resBodyWithEncrypt, aesKey, aesIV };
|
||||
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] res: ${JSON.stringify(resBody)}`)
|
||||
} else {
|
||||
ctx.body = { result: aesEncrypt(JSON.stringify({ status: 3, data: 'internal err' }), aesKey, aesIV) };
|
||||
ctx.body = { result: msgEncrypt.encryptMsg({ status: 3, data: 'internal err' }), aesKey, aesIV };
|
||||
ctx.service.utils.log('ERROR', `[${ctx.request.url}] [${ctx.logcode}] res: ${resBody}`)
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user