Skip to content

Commit 3f14592

Browse files
committed
improve code
1 parent d45a408 commit 3f14592

File tree

4 files changed

+107
-95
lines changed

4 files changed

+107
-95
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "json-secure-crypt",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "A simple Node.js tool to encrypt and decrypt JSON files",
5-
"main": "src/encrypt-json.js",
5+
"main": "src/index.js",
66
"scripts": {
77
"encrypt": "node src/encrypt-json.js",
88
"decrypt": "node src/decrypt-json.js"

src/decrypt-json.js

+51-57
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
#!/usr/bin/env node
2+
13
const fs = require('fs');
24
const CryptoJS = require('crypto-js');
35
const axios = require('axios');
46
const { HttpsProxyAgent } = require('https-proxy-agent');
57

6-
// 解密函数,暴露给外部使用
78
function decryptJson(encryptedContent, secretKey) {
89
const decrypted = CryptoJS.AES.decrypt(encryptedContent, secretKey);
910
return decrypted.toString(CryptoJS.enc.Utf8);
1011
}
1112

12-
// 通过HTTP获取远程JSON文件并解密
1313
async function decryptJsonFromUrl(url, secretKey, proxyConfig = null) {
1414
try {
1515
const axiosConfig = {};
@@ -34,76 +34,26 @@ async function decryptJsonFromUrl(url, secretKey, proxyConfig = null) {
3434
}
3535
}
3636

37-
// 解析命令行参数
38-
const args = process.argv.slice(2);
39-
let inputFilePath = null;
40-
let outputFilePath = './decrypted.json';
41-
let secretKey = 'your-default-secret-key';
42-
let url = null;
43-
let proxyConfig = null;
44-
45-
// 参数处理
46-
args.forEach((arg, index) => {
47-
if (arg.startsWith('http')) {
48-
url = arg; // 如果第一个参数是URL
49-
} else if (index === 0 && !arg.startsWith('http') && !arg.includes(':')) {
50-
inputFilePath = arg; // 第一个参数是本地文件路径
51-
} else if (index === 1 && !arg.includes(':')) {
52-
outputFilePath = arg; // 第二个参数是输出文件路径
53-
} else if (index === 2 && !arg.includes(':')) {
54-
secretKey = arg; // 第三个参数是密钥
55-
} else if (arg.includes(':')) {
56-
// 处理代理配置参数
57-
const [host, port] = arg.split(':');
58-
proxyConfig = {
59-
host: host,
60-
port: parseInt(port, 10)
61-
};
37+
function decryptJsonFile(inputFilePath, outputFilePath = './decrypted.json', secretKey = 'your-default-secret-key') {
38+
if (!inputFilePath) {
39+
throw new Error('Please provide an input file path.');
6240
}
63-
});
64-
65-
// 检查是否提供了输入文件路径或URL
66-
if (!inputFilePath && !url) {
67-
console.error('Please provide an input file path or URL.');
68-
process.exit(1);
69-
}
7041

71-
if (url) {
72-
// 如果提供了URL,从远程获取并解密JSON
73-
decryptJsonFromUrl(url, secretKey, proxyConfig)
74-
.then(decryptedContent => {
75-
console.log('Decrypted JSON content:', decryptedContent);
76-
fs.writeFile(outputFilePath, decryptedContent, 'utf8', (err) => {
77-
if (err) {
78-
console.error('Error writing the decrypted JSON file:', err);
79-
return;
80-
}
81-
console.log('Decrypted JSON has been saved to:', outputFilePath);
82-
});
83-
})
84-
.catch(err => {
85-
console.error('Error during decryption from URL:', err);
86-
});
87-
} else {
88-
// 读取加密后的JSON文件并解密
8942
fs.readFile(inputFilePath, 'utf8', (err, data) => {
9043
if (err) {
9144
console.error('Error reading the encrypted JSON file:', err);
9245
return;
9346
}
9447

9548
try {
96-
// 解析加密的JSON内容
9749
const encryptedContent = JSON.parse(data).encrypted;
9850

9951
if (!encryptedContent) {
10052
throw new Error('No encrypted content found in the JSON file.');
10153
}
10254

103-
// 使用解密函数解密内容
10455
const decryptedString = decryptJson(encryptedContent, secretKey);
10556

106-
// 将解密后的内容写入新的JSON文件
10757
fs.writeFile(outputFilePath, decryptedString, 'utf8', (err) => {
10858
if (err) {
10959
console.error('Error writing the decrypted JSON file:', err);
@@ -117,8 +67,52 @@ if (url) {
11767
});
11868
}
11969

120-
// 导出解密函数
70+
// 如果是命令行调用
71+
if (require.main === module) {
72+
const args = process.argv.slice(2);
73+
let inputFilePath = null;
74+
let outputFilePath = './decrypted.json';
75+
let secretKey = 'your-default-secret-key';
76+
let url = null;
77+
let proxyConfig = null;
78+
79+
args.forEach((arg, index) => {
80+
if (arg.startsWith('http')) {
81+
url = arg;
82+
} else if (index === 0 && !arg.startsWith('http') && !arg.includes(':')) {
83+
inputFilePath = arg;
84+
} else if (index === 1 && !arg.includes(':')) {
85+
outputFilePath = arg;
86+
} else if (index === 2 && !arg.includes(':')) {
87+
secretKey = arg;
88+
} else if (arg.includes(':')) {
89+
const [host, port] = arg.split(':');
90+
proxyConfig = { host, port: parseInt(port, 10) };
91+
}
92+
});
93+
94+
if (url) {
95+
decryptJsonFromUrl(url, secretKey, proxyConfig)
96+
.then(decryptedContent => {
97+
console.log('Decrypted JSON content:', decryptedContent);
98+
fs.writeFile(outputFilePath, decryptedContent, 'utf8', (err) => {
99+
if (err) {
100+
console.error('Error writing the decrypted JSON file:', err);
101+
return;
102+
}
103+
console.log('Decrypted JSON has been saved to:', outputFilePath);
104+
});
105+
})
106+
.catch(err => {
107+
console.error('Error during decryption from URL:', err);
108+
});
109+
} else {
110+
decryptJsonFile(inputFilePath, outputFilePath, secretKey);
111+
}
112+
}
113+
121114
module.exports = {
122115
decryptJson,
123-
decryptJsonFromUrl
116+
decryptJsonFromUrl,
117+
decryptJsonFile
124118
};

src/encrypt-json.js

+45-36
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1+
#!/usr/bin/env node
2+
13
const fs = require('fs');
24
const CryptoJS = require('crypto-js');
3-
const path = require('path');
45

5-
// 从命令行参数获取输入文件、输出文件路径和加密密钥
6-
const inputFilePath = process.argv[2];
7-
const outputFilePath = process.argv[3] || './encrypted.json';
8-
const secretKey = process.argv[4] || 'your-default-secret-key';
6+
// 加密函数
7+
function encryptJsonFile(inputFilePath, outputFilePath = './encrypted.json', secretKey = 'your-default-secret-key') {
8+
if (!inputFilePath) {
9+
throw new Error('Please provide an input file path.');
10+
}
11+
12+
// 读取JSON文件
13+
fs.readFile(inputFilePath, 'utf8', (err, data) => {
14+
if (err) {
15+
console.error('Error reading the JSON file:', err);
16+
return;
17+
}
18+
19+
try {
20+
const jsonContent = JSON.parse(data);
21+
const jsonString = JSON.stringify(jsonContent);
22+
const encrypted = CryptoJS.AES.encrypt(jsonString, secretKey).toString();
923

10-
// 检查是否提供了输入文件路径
11-
if (!inputFilePath) {
12-
console.error('Please provide an input file path.');
13-
process.exit(1);
24+
fs.writeFile(outputFilePath, JSON.stringify({ encrypted }), 'utf8', (err) => {
25+
if (err) {
26+
console.error('Error writing the encrypted JSON file:', err);
27+
return;
28+
}
29+
console.log('JSON file has been encrypted and saved to:', outputFilePath);
30+
});
31+
} catch (error) {
32+
console.error('Error processing JSON data:', error);
33+
}
34+
});
1435
}
1536

16-
// 读取JSON文件
17-
fs.readFile(inputFilePath, 'utf8', (err, data) => {
18-
if (err) {
19-
console.error('Error reading the JSON file:', err);
20-
return;
21-
}
37+
// 如果是命令行调用
38+
if (require.main === module) {
39+
const inputFilePath = process.argv[2];
40+
const outputFilePath = process.argv[3] || './encrypted.json';
41+
const secretKey = process.argv[4] || 'your-default-secret-key';
2242

23-
try {
24-
// 解析JSON文件内容
25-
const jsonContent = JSON.parse(data);
26-
27-
// 将JSON对象转换为字符串
28-
const jsonString = JSON.stringify(jsonContent);
29-
30-
// 使用AES加密
31-
const encrypted = CryptoJS.AES.encrypt(jsonString, secretKey).toString();
32-
33-
// 将加密后的内容写入新的JSON文件
34-
fs.writeFile(outputFilePath, JSON.stringify({ encrypted }), 'utf8', (err) => {
35-
if (err) {
36-
console.error('Error writing the encrypted JSON file:', err);
37-
return;
38-
}
39-
console.log('JSON file has been encrypted and saved to:', outputFilePath);
40-
});
41-
} catch (error) {
42-
console.error('Error processing JSON data:', error);
43+
if (!inputFilePath) {
44+
console.error('Please provide an input file path.');
45+
process.exit(1);
4346
}
44-
});
47+
48+
encryptJsonFile(inputFilePath, outputFilePath, secretKey);
49+
}
50+
51+
module.exports = {
52+
encryptJsonFile
53+
};

src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { encryptJsonFile } = require('./encrypt-json');
2+
const { decryptJson, decryptJsonFromUrl, decryptJsonFile } = require('./decrypt-json');
3+
4+
module.exports = {
5+
encryptJsonFile,
6+
decryptJson,
7+
decryptJsonFromUrl,
8+
decryptJsonFile
9+
};

0 commit comments

Comments
 (0)