-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypt.java
More file actions
130 lines (117 loc) · 3.12 KB
/
Copy pathEncrypt.java
File metadata and controls
130 lines (117 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Encrypt {
/**
* @author crazykay
* @see "在异或运算中,对于两个变量 x,y 满足 (x^y^y)==x"
*/
// 加密数值(可随意设定)
private static final int PASSWORD = 01234567;
// 设置加密文件后缀(可随意设定)
private static final String SUFFIX = ".enc";
// path指定加密文件路径
public void doEncrypt(String path) {
FileInputStream in = null;
FileOutputStream out = null;
try {
int tmp = 0;
File inFile = new File(path);
in = new FileInputStream(inFile);
File outFile = new File(path + SUFFIX);
out = new FileOutputStream(outFile);
while ((tmp = in.read()) != -1) {
// 加密处理
out.write(tmp ^ PASSWORD);
}
} catch (FileNotFoundException e) {
System.err.println("文件不存在!!");
} catch (IOException e) {
System.err.println("文件读取异常!!");
} finally {
// finally块关闭流操作
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
System.err.println("关闭流失败!!");
}
}
}
// path指定解密文件路径
public void doDecrypt(String path) {
int index = path.lastIndexOf(SUFFIX);
if (index != path.length() - SUFFIX.length()) {
System.err.println("文件类型不正确!!");
return;
}
FileInputStream in = null;
FileOutputStream out = null;
try {
int tmp = 0;
File inFile = new File(path);
in = new FileInputStream(inFile);
String name = path.substring(0, index);
File outFile = new File(name);
out = new FileOutputStream(outFile);
while ((tmp = in.read()) != -1) {
// 再次异或PASSWORD实现解密
out.write(tmp ^ PASSWORD);
}
} catch (FileNotFoundException e) {
System.err.println("文件不存在!!");
} catch (IOException e) {
System.err.println("文件读取异常!!");
} finally {
// finally块关闭流操作
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
System.err.println("关闭流失败!!");
}
}
}
public static void main(String[] args) {
Encrypt encrypt = new Encrypt();
// 第一个参数指定操作(加密/解密)
// 第二个参数制定文件所在路径
if (args.length != 2) {
System.err.println("参数设置不正确");
System.out.println();
System.out.println("java Encrypt [OPTION] [FILE PATH]");
System.out.println("\tencrypt\t-e\t加密\n\tdecrypt\t-d\t解密");
return;
}
switch (args[0]) {
case "encrypt":
encrypt.doEncrypt(args[1]);
break;
case "-e":
encrypt.doEncrypt(args[1]);
break;
case "decrypt":
encrypt.doDecrypt(args[1]);
break;
case "-d":
encrypt.doDecrypt(args[1]);
break;
default:
System.err.println("参数设置不正确");
System.out.println();
System.out.println("java Encrypt [OPTION] [FILE PATH]");
System.out.println("\tencrypt\t-e\t加密\n\tdecrypt\t-d\t解密");
break;
}
}
}