This commit is contained in:
2025-02-18 08:31:38 +08:00
parent b6a71aa49b
commit cf1cc83547
12 changed files with 104 additions and 52 deletions

View File

@@ -10,7 +10,9 @@ module.exports = {
if(req.headers.hasOwnProperty("data")) {
try {
req.headers.data = rsa.PrivateKeyDecryption(req.headers.data)
if(req.url == config.filter.login || req.url == config.filter.add) {
if(req.url == config.filter.login || req.url == config.filter.add
|| req.url == config.filter.home
) {
next()
} else {
if(req.headers.hasOwnProperty("token")) {
@@ -32,6 +34,11 @@ module.exports = {
}
}
} catch (e) {
console.log("--------");
console.log("e --------: ", e);
console.log("--------");
res.json({
status: 500,
message: '抱歉,公钥加密有误,请检查公钥'

View File

@@ -0,0 +1,53 @@
const NodeRsa = require("node-rsa");
const fs = require("fs");
const path = require("path");
const config = require("../config");
class Rsa {
constructor() {
this.private_key = this.readFiles(this.getFilePath(config.rsa.private_key))
this.public_key = this.readFiles(this.getFilePath(config.rsa.public_key))
}
// 公钥加密
PublicKeyEncryption(text) {
return (new NodeRsa(this.public_key)).encrypt(JSON.stringify(text), 'base64')
}
// 公钥解密
PublicKeyDecryption(text) {
return JSON.parse((new NodeRsa(this.public_key)).decryptPublic(text, 'utf8'))
}
// 私钥加密
PrivateKeyEncryption(text) {
return (new NodeRsa(this.private_key)).encryptPrivate(JSON.stringify(text), 'base64')
}
// 私钥解密
PrivateKeyDecryption(text) {
return JSON.parse((new NodeRsa(this.private_key)).decrypt(text, 'utf8'))
}
/**
* 获取path.join文件地址
* @param paths
* @returns {string}
*/
getFilePath(paths) {
return path.join(__dirname, paths)
}
/**
* 读取文件数据
* @param path
* @returns {*}
*/
readFiles(path) {
return fs.readFileSync(path).toString()
}
}
module.exports = new Rsa();

View File

@@ -7,6 +7,15 @@ class Rsa {
constructor() {
this.private_key = this.readFiles(this.getFilePath(config.rsa.private_key))
this.public_key = this.readFiles(this.getFilePath(config.rsa.public_key))
this.pri_rsa = new NodeRsa(this.private_key,'pkcs8-private-pem')
this.pri_rsa.setOptions({ encryptionScheme: 'pkcs1' })
this.pri_rsa.setOptions({ environment: "browser" });
this.pub_rsa = new NodeRsa(this.public_key,'pkcs8-public-pem')
this.pub_rsa.setOptions({ encryptionScheme: 'pkcs1' })
this.pub_rsa.setOptions({ environment: "browser" });
}
@@ -28,7 +37,7 @@ class Rsa {
// 私钥解密
PrivateKeyDecryption(text) {
return JSON.parse((new NodeRsa(this.private_key)).decrypt(text, 'utf8'))
return JSON.parse(this.pri_rsa.decrypt(text, 'utf8'))
}
/**