Java小程序:超强Java加密程序

这款小程序是可以根据用户的输入对硬盘中指定目录下的文件进行加密。用户如果对文件进行重复加密则会提示未操作,而文件也将不再次做加密;如果用户对为加密的内容进行解密,程序也将提示未操作,而文件也将不进行解密操作;如果用户在解密过程中输入的密钥是错误的,那么将无法进行解密操作,程序也会提示未操作。

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Crazy {
private static Scanner sc = new Scanner(System.in);
private static String filePath = “”;
private static int key = 0;
private static String bombom = “”; //,
private static byte[] keyArr;
private static String basicLock = “”;
private static String basicKey = “91,80,111,119,101,114,32,66,121,32,76,105,72,117,97,110,93,”;
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
while (true) {
println(“****************************************************”);
println(“*————————————————–*”);
println(“*——–接下来发生的事情有可能出乎你的意料——–*”);
println(“*———————准备开始———————*”);
println(“*————————————————–*”);
println(“****************************************************”);
println(“”);
String tempSC = scanner(“请输入要操作的路径:”);
filePath = tempSC;
File file = getFile(filePath);
if (file.exists()) {
if (file.isDirectory()) {
boolean isEncrypt;
while (true) {
println(“****************************************************”);
println(“*————————————————–*”);
tempSC = scanner(“*————你要进行什么操作(加密/解密)———–*”);
if (“jiami”.equals(tempSC) || “加密”.equals(tempSC)) {
isEncrypt = true;
break;
} else if (“jiemi”.equals(tempSC) || “解密”.equals(tempSC)) {
isEncrypt = false;
break;
} else {
//println(“*********************请重新输入*********************”);
}
}
println(“”);
println(“****************************************************”);
println(“*————————————————–*”);
tempSC = scanner(“*—————–请输入KEY(密钥)——————*”);
bombom = “”;
keyArr = tempSC.getBytes();
key = 0;
for (int i = 0; i < keyArr.length; i++) {
bombom += ((256 – Math.abs(keyArr[i])) ^ 1) + “,”;
key += (256 – Math.abs(keyArr[i]));
}
key = Math.abs(key);
if (isEncrypt) {
encrypt(file);
} else {
decrypt(file);
}
} else {
//println(“你输入的是文件路径!”);
}
} else {
println(“路径不存在!”);
}
println(“—————————————————-“);
println(“****************************************************”);
println(“”);
}
}
public static boolean checkFile(BufferedInputStream bis,String checkType) throws IOException {
String sss = “”;
String[] sArr = bombom.split(“,”);
basicLock = “”;
int b;
for (int i = 0; i < basicKey.split(“,”).length; i++) {
if ((b = bis.read()) != –1) {
basicLock += (b + “,”);
} else {
//break;
}
}
if (“encrypt”.equals(checkType)) {
if (basicKey.equals(basicLock)) {
//基础密钥一致,说明是已经经过加密的,因此返回false跳过,不再加密处理
return false;
} else {
//基础密钥不一致
//进行加密
return true;
}
} else if (“decrypt”.equals(checkType)) {
if (basicKey.equals(basicLock)) {
//基础密钥一致
int forNum = bis.read();
if (forNum == –1) {
return false; //跳出
} else {
for (int j = 0; j < forNum; j++) {
if ((b = bis.read()) != –1) {
sss += (b + “,”);
} else {
break;
}
}
if (sss.equals(bombom)) {
return true;
} else {
return false;
}
}
} else {
//基础密钥不一致,说明文件没有经过加密,因此无需进行加密处理,直接返回false跳过
return false;
}
} else {
println(“未知错误!”);
return false;
}
}
public static void encrypt(File file) throws NumberFormatException, IOException {
File[] fileLists = getFileLists(file);
if (fileLists != null) {
for (File f : fileLists) {
if (f.isDirectory()) {
encrypt(f);
} else {
FileInputStream fis = new FileInputStream(f.getPath());
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(f.getPath() + “.lihuan”);
BufferedOutputStream bos = new BufferedOutputStream(fos);
if (checkFile(bis, “encrypt”)) {
bis.close();
int b;
String[] sArr = basicKey.split(“,”);
for (String s : sArr) {
bos.write(Integer.parseInt(s));
}
bos.write(keyArr.length);
sArr = bombom.split(“,”);
for (String s : sArr) {
bos.write(Integer.parseInt(s));
}
fis = new FileInputStream(f.getPath());
bis = new BufferedInputStream(fis);
while ((b = bis.read()) != –1) {
b = b ^ key;
bos.write(b);
}
bos.close();
bis.close();
File fileCopy = new File(f.getPath() + “.lihuan”);
String filePath = f.getPath();
System.out.println(f.getPath() + “[加密成功]”);
f.delete();
File fileNew = new File(filePath);
fileCopy.renameTo(fileNew);
} else {
System.out.println(f.getPath() + “[未操作]”);
bos.close();
bis.close();
File fileCopy = new File(f.getPath() + “.lihuan”);
fileCopy.delete();
}
}
}
}
}
public static void decrypt(File file) throws NumberFormatException, IOException {
File[] fileLists = getFileLists(file);
if (fileLists != null) {
for (File f : fileLists) {
if (f.isDirectory()) {
decrypt(f);
} else {
FileInputStream fis = new FileInputStream(f.getPath());
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(f.getPath() + “.lihuan”);
BufferedOutputStream bos = new BufferedOutputStream(fos);
if (checkFile(bis, “decrypt”)) {
int b;
while ((b = bis.read()) != –1) {
b = b ^ key;
bos.write(b);
}
bos.close();
bis.close();
File fileCopy = new File(f.getPath() + “.lihuan”);
String filePath = f.getPath();
println(f.getPath() + “[解密成功]”);
f.delete();
File fileNew = new File(filePath);
fileCopy.renameTo(fileNew);
} else {
println(f.getPath() + “[未操作]”);
bos.close();
bis.close();
File fileCopy = new File(f.getPath() + “.lihuan”);
fileCopy.delete();
}
}
}
}
}
public static File getFile(String filePath) {
File file = new File(filePath);
return file;
}
public static File[] getFileLists(File file) {
File[] fileLists = file.listFiles();
return fileLists;
}
public static String scanner(String message) {
println(message);
String tempStr = sc.nextLine();
if (“exit”.equals(tempStr.toLowerCase()) || “tuichu”.equals(tempStr.toLowerCase()) || “退出”.equals(tempStr.toLowerCase())) {
System.out.println(“程序正在退出……”);
System.exit(0);
} else {
}
return tempStr;
}
public static void print(String str) {
System.out.print(str);
}
public static void println(String str) {
System.out.println(str);
print(“”);
}
}

如果程序有任何BUG,欢迎反馈。