0%

文件加密工具

文章字数:1045,阅读全文大约需要4分钟

主要功能是将指定文件加密到加密库中,以及从加密库中解密相应文件

相关文件

  • ShaUtil.java sha256工具类,主要用于密码签名成aes256的秘钥
  • AesUtil.java加密工具类
  • EncryptionFileProcessor.java单个文件加密,调用前面的工具类加密文件
  • FileSearchStream.java文件搜索主类,搜索根目录下的文件
  • EncryptionDirectoryProcessor.java调用搜索和加密类完成文件夹加密解密,查看操作
  • Test.java测试及演示类

加密相关工具

  • ShaUtil.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
package com.colin.tool.crypto;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* sha256工具
*
* @author colin.cheng
* @date 2021-11-11
* @since 1.0.0
*/
public class ShaUtil {

private static final String KEY_ALGORITHM = "SHA-256";
private static final String DEFAULT_CHARSET = "UTF-8";

/**
* sha256签名
*
* @param str
* @return
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static byte[] encodingStr(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest messageDigest;
messageDigest = MessageDigest.getInstance(KEY_ALGORITHM);
messageDigest.update(str.getBytes(DEFAULT_CHARSET));
return messageDigest.digest();
}
}
  • AesUtil.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
package com.colin.tool.crypto;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
/**
* AES加密工具类
*
* @author colin.cheng
* @date 2021-11-11 10:11
* @since 1.0.0
*/
public class AesUtil {

/** 算法名 */
private static final String KEY_ALGORITHM = "AES";
/** 默认的加密算法及模式 */
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

/**
* AES 加密操作
*
* @param byteContent
* 待加密内容
* @param key
* 加密密钥
* @return 加密数据
*/
public static byte[] encrypt(byte[] byteContent, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
// 创建密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// 初始化为加密模式的密码器
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
// 加密
return cipher.doFinal(byteContent);
}

public static void encryptByStream(InputStream from, OutputStream to, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException {
// 创建密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// 初始化为加密模式的密码器
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
try (CipherInputStream cipherInputStream = new CipherInputStream(from, cipher)) {
int l;
byte[] cache = new byte[16 * 100];
while ((l = cipherInputStream.read(cache)) != -1) {
to.write(cache, 0, l);
to.flush();
}
}
}

public static void decryptByStream(InputStream from, OutputStream to, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
try (CipherOutputStream cipherOutputStream = new CipherOutputStream(to, cipher)){
int l;
byte[] cache = new byte[16 * 100];
while ((l = from.read(cache)) != -1) {
cipherOutputStream.write(cache, 0, l);
}
}
}

/**
* AES 加密操作,返回Base64字符串
*
* @param byteContent
* 待加密内容
* @param key
* 加密密钥
* @return Base64转码后的加密数据
*/
public static String base64Encrypt(byte[] byteContent, byte[] key) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
byte[] encrypt = encrypt(byteContent, key);
if (encrypt != null) {
return Base64.getEncoder().encodeToString(encrypt);
}
return null;
}

/**
* AES 解密操作
*
* @param content
* 密文
* @param key
* 秘钥
* @return 解密后的数据
*/
public static byte[] decrypt(byte[] content, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
// 实例化
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// 使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
// 执行操作
return cipher.doFinal(content);
}

/**
* AES 解密操作
*
* @param content
* base64形式的密文
* @param key
* 秘钥
* @return 解密后的数据
*/
public static byte[] decrypt(String content, byte[] key) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
if (content != null) {
byte[] decode = Base64.getDecoder().decode(content);
return decrypt(decode, key);
}
return null;
}

/**
* 生成加密秘钥
*
* @return 生成指定算法密钥生成器的 KeyGenerator 对象
*/
private static SecretKeySpec getSecretKey(final byte[] key) {
return new SecretKeySpec(key, KEY_ALGORITHM);
}
}

文件加密相关工具类

  • EncryptionFileProcessor.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
234
package com.colin.tool.file;

import com.colin.tool.crypto.AesUtil;
import com.colin.tool.crypto.ShaUtil;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;

/**
* 单个文件加密解密相关工具
*
* @author colin.cheng
* @date 2021-11-11
* @since 1.0.0
*/
public class EncryptionFileProcessor {

/** 全局秘钥 */
private byte[] key = null;
/** 默认是否保存源文件 */
private boolean saveResource = false;
/** 默认保存位置为源文件所在文件夹,可以自定义 */
private String toPath = null;

private static String cryptoSuffix = ".cl";
private static String cryptoPrefix = "cry-";

public EncryptionFileProcessor(String pwd) throws UnsupportedEncodingException, NoSuchAlgorithmException {
this.key = pwd != null ? ShaUtil.encodingStr(pwd) : null;
}

/**
* 设置全局秘钥
*
* @param pwd
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
*/
public EncryptionFileProcessor setPwd(String pwd) throws UnsupportedEncodingException, NoSuchAlgorithmException {
this.key = pwd != null ? ShaUtil.encodingStr(pwd) : null;
return this;
}

/**
* 设置加密、解密生成的文件存放文件夹
*
* @param toPath
*/
public void setToPath(String toPath) {
this.toPath = toPath;
}

/**
* 设置是否保存源文件
*
* @param saveResource
*/
public EncryptionFileProcessor setSaveResource(boolean saveResource) {
this.saveResource = saveResource;
return this;
}

/**
* 加密文件
*
* @param resource
* @throws Exception
*/
public void encryptFile(File resource) throws Exception {
encryptFile(resource, key, saveResource, this.toPath);
}

/**
* 解密文件
*
* @param resource
* @throws Exception
*/
public void decryptFile(File resource) throws Exception {
decryptFile(resource, key, saveResource, this.toPath);
}

/**
* 加密文件名
*
* @param fileName
* @return
* @throws Exception
*/
public String encryptFileName(String fileName) throws Exception {
return encryptFileName(fileName, key);
}

/**
* 解密文件名
*
* @param fileName
* @return
* @throws Exception
*/
public String decryptFileName(String fileName) throws Exception {
return decryptFileName(fileName, key);
}

// static

/**
* 加密文件
*
* @param resource 源文件
* @param pwd 密码
* @param saveResource 加密之后是否删除源文件
* @param toPath 存放位置,空则放在源文件所在文件夹
* @throws Exception
*/
public static void encryptFile(File resource, String pwd, boolean saveResource, String toPath) throws Exception {
final byte[] key = ShaUtil.encodingStr(pwd);
encryptFile(resource, key, saveResource, toPath);
}

/**
* 解密文件
*
* @param resource 加密文件
* @param pwd 密码
* @param saveResource 解密之后是否删除加密文件
* @param toPath 存放位置,空则放在源文件所在文件夹
* @throws Exception
*/
public static void decryptFile(File resource, String pwd, boolean saveResource, String toPath) throws Exception {
final byte[] key = ShaUtil.encodingStr(pwd);
decryptFile(resource, key, saveResource, toPath);
}


/**
* 加密文件名
*
* @param fileName
* @param pwd
* @return
* @throws Exception
*/
public static String encryptFileName(String fileName, String pwd) throws Exception {
final byte[] key = ShaUtil.encodingStr(pwd);
return encryptFileName(fileName, key);
}

/**
* 解密文件名
*
* @param fileName
* @param pwd
* @return
* @throws Exception
*/
public static String decryptFileName(String fileName, String pwd) throws Exception {
final byte[] key = ShaUtil.encodingStr(pwd);
return decryptFileName(fileName, key);
}

// private

private static void decryptFile(File resource, byte[] key, boolean saveResource, String toPath) throws Exception {
if(resource.exists() && isEncryptedFileName(resource.getName())) {
assert key != null;
try (InputStream inputStream = new FileInputStream(resource)) {
final String encryptName = resource.getName();
final String fileName = decryptFileName(encryptName, key);
if(toPath == null || "".equals(toPath.trim())) {
toPath = resource.getParent();
}
final File file = new File(toPath + File.separator + fileName);
if(file.exists()) {
throw new RuntimeException("decrypt file is exists, encrypt name = [" + encryptName + "], decrypt name = [" + fileName + "]");
}
try (final FileOutputStream outputStream = new FileOutputStream(file)) {
AesUtil.decryptByStream(inputStream, outputStream, key);
}
}
if(!saveResource) {
resource.delete();
}
}
}

private static void encryptFile(File resource, byte[] key, boolean saveResource, String toPath) throws Exception {
if(resource.exists() && !isEncryptedFileName(resource.getName())) {
assert key != null;
try (FileInputStream inputStream = new FileInputStream(resource)) {
final String fileName = encryptFileName(resource.getName(), key);
if(toPath == null || "".equals(toPath.trim())) {
toPath = resource.getParent();
}
final File encryptFile = new File(toPath + File.separator + fileName);
if(encryptFile.exists()) {
throw new RuntimeException("encrypt file is exists! resource name = [" + fileName + "]");
}
try (final FileOutputStream outputStream = new FileOutputStream(encryptFile)){
AesUtil.encryptByStream(inputStream, outputStream, key);
}
}
if(!saveResource) {
resource.delete();
}
}
}

private static String encryptFileName(String fileName, byte[] key) throws Exception {
assert key != null;
if(!isEncryptedFileName(fileName)) {
String encrypt = AesUtil.base64Encrypt(fileName.getBytes(StandardCharsets.UTF_8), key);
assert encrypt != null;
return cryptoPrefix + encrypt.replace("/", "#") + cryptoSuffix;
}
return fileName;
}

private static String decryptFileName(String fileName, byte[] key) throws Exception {
assert key != null;
if(isEncryptedFileName(fileName)) {
fileName = fileName.replace("#", "/").replace(cryptoSuffix, "").replace(cryptoPrefix, "");
final byte[] decrypt = AesUtil.decrypt(fileName, key);
assert decrypt != null;
return new String(decrypt, StandardCharsets.UTF_8);
}
return fileName;
}

private static boolean isEncryptedFileName(String fileName) {
return fileName.startsWith(cryptoPrefix) && fileName.endsWith(cryptoSuffix);
}
}
  • FileSearchStream.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
package com.colin.tool.file;

import java.io.File;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* 文件搜索流
*
* @author colin.cheng
* @since 1.0.0
*/
public class FileSearchStream {

private File[] root;
private boolean parallel;
private Consumer<File> afterConsumer;
private Function<File, Boolean> filter;
private boolean deep = true;

private FileSearchStream() {}

public FileSearchStream(File... root) {
init(root);
}

/**
* 获取一个查询对象
*
* @param root
* @return
*/
public static FileSearchStream of(File... root) {
return new FileSearchStream(root);
}

public boolean isDeep() {
return deep;
}

/** 如果是文件夹,是否遍历 */
public FileSearchStream setDeep(boolean deep) {
this.deep = deep;
return this;
}

/**
* 获取本地所有盘符
*/
public static File[] getSystemRoot() {
return File.listRoots();
}

/**
* 过滤条件
*
* @param filter
* @return
*/
public FileSearchStream filter(Function<File, Boolean> filter) {
this.filter = filter;
return this;
}

/**
* 找到之后如何处理
*
* @param afterConsumer
* @return
*/
public void forEach(Consumer<File> afterConsumer) {
this.afterConsumer = afterConsumer;
searchRoot();
}

/**
* 转换成list
*
* @param listFunction
* @param <T>
* @return
*/
public <T> List<T> toList(Function<File, T> listFunction) {
List<T> list = new LinkedList<>();
this.afterConsumer = file -> list.add(listFunction.apply(file));
searchRoot();
return list;
}

/**
* 分组保存
*
* @param keyFunction
* @param valFunction
* @param <T>
* @param <R>
* @return
*/
public <T, R> Map<T, List<R>> groupingBy(Function<File, T> keyFunction, Function<File, R> valFunction) {
Map<T, List<R>> res = new LinkedHashMap<>();
this.afterConsumer = file -> {
T key = keyFunction.apply(file);
R val = valFunction.apply(file);
if (res.containsKey(key)) {
res.get(key).add(val);
} else {
List<R> list = new LinkedList<>();
list.add(val);
res.put(key, list);
}
};
searchRoot();
return res;
}

/**
* 根据父级文件夹分组
*
* @return
*/
public Map<String, List<String>> groupingByPath() {
return groupingBy(File::getParent, File::getName);
}

/**
* 重置
*
* @return
*/
public FileSearchStream reset(File... root) {
init(root);
return this;
}

/**
* 初始化
*
* @param root
*/
private void init(File... root) {
this.root = root;
this.parallel = false;
this.afterConsumer = null;
this.filter = (file) -> true;
}

/**
* 开始搜索
*
* @return
*/
private void searchRoot() {
final Stream<File> fileStream = Arrays.stream(root);
if (parallel) {
fileStream.parallel().forEach(this::search);
} else {
fileStream.forEach(this::search);
}
}

/**
* 获取对应盘符下符合要求的文件
*/
private void search(File file) {
File[] f = file.listFiles();
if (f != null) {
if (f.length > 0) {
for (File files : f) {
if(deep) {
if (files.isDirectory()) {
search(files);
} else {
if (filter.apply(files)) {
if (afterConsumer != null) {
afterConsumer.accept(files);
}
}
}
} else {
if (filter.apply(files)) {
if (afterConsumer != null) {
afterConsumer.accept(files);
}
}
}
}
}
}
}

/**
* 文件后缀
*/
public enum FileSuffix {
/** 图片 */
IMG("psd", "pdd", "gif", "jpeg", "jpg", "png"),
/** 文档 */
DOCUMENT("doc", "docx", "xls", "xlsx", "csv", "ppt", "pptx", "txt"),
/** 程序 */
APPLICATION("exe", "bat", "cmd", "sh", "ink", "py", "class", "java"),
/** 音频 */
AUDIO("mp3", "flac", "ape", "cd", "wave", "aiff"),
/** 视频 */
VIDEO(
"avi", "mov", "qt", "asf", "rm", "navi", "divX", "mpeg", "mpg", "ogg", "mod", "rmvb", "flv", "mp4", "3gp");

private String[] suffix;

FileSuffix(String... suffix) {
this.suffix = suffix;
}

public boolean check(String fileName) {
if (fileName.contains(".")) {
String fileSuffix = fileName.split("\\.")[1];
fileSuffix = fileSuffix.toLowerCase();
for (String s : suffix) {
String b = s.toLowerCase();
if (fileSuffix.equals(b)) {
return true;
}
}
}
return false;
}

public String[] getSuffix() {
return suffix;
}
}
}
  • EncryptionDirectoryProcessor.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
package com.colin.tool.file;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

/**
* 文件夹加密解密工具
*
* @author colin.cheng
* @date 2021-12-03
* @since 1.0.0
*/
public class EncryptionDirectoryProcessor {

private EncryptionFileProcessor processor;

private Consumer<File> searchLog = null;
private Consumer<File> processorLog = null;

/**
* 新建一个文件夹加密处理器
*
* @param pwd 密码
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
*/
public EncryptionDirectoryProcessor(String pwd) throws UnsupportedEncodingException, NoSuchAlgorithmException {
this.processor = new EncryptionFileProcessor(pwd);
}

/**
* 是否保存源文件
*
* @param saveResource
* @return
*/
public EncryptionDirectoryProcessor setSaveResource(boolean saveResource) {
this.processor.setSaveResource(saveResource);
return this;
}

/**
* 设置文件存放位置,默认是源文件所在文件夹
*
* @param toPath
* @return
*/
public EncryptionDirectoryProcessor setToPath(String toPath) {
this.processor.setToPath(toPath);
return this;
}

/**
* 加密文件夹
*
* @param path
* @throws Exception
*/
public void encryptDirectory(String path) throws Exception {
assert this.processor != null;
encryptDirectory(path, this.processor, this.searchLog, this.processorLog);
}

/**
* 解密文件夹
*
* @param path
* @throws Exception
*/
public void decryptDirectory(String path) throws Exception {
assert this.processor != null;
decryptDirectory(path, this.processor, this.searchLog, this.processorLog);
}

/**
* 解密并查看文件夹内文件名
*
* @param path
* @param deep 如果内部包含文件夹,是否遍历文件夹
* @return
* @throws Exception
*/
public List<String> listDirectory(String path, boolean deep) throws Exception {
return listDirectory(path, this.processor, deep);
}

public Consumer<File> getSearchLog() {
return searchLog;
}

public void setSearchLog(Consumer<File> searchLog) {
this.searchLog = searchLog;
}

public Consumer<File> getProcessorLog() {
return processorLog;
}

public void setProcessorLog(Consumer<File> processorLog) {
this.processorLog = processorLog;
}

AtomicLong index;
AtomicLong processorIndex = new AtomicLong();

public EncryptionDirectoryProcessor defaultLog() {
this.index = new AtomicLong();
this.processorIndex = new AtomicLong();
this.setSearchLog(file ->
System.out.println("[" + index.incrementAndGet() + "] file find, path = [" + file.getPath() + "]"));
this.setProcessorLog(file ->
System.out.println("[" + processorIndex.incrementAndGet() + "/" + index.get() + "] file is Processor " + ((int) (((double) processorIndex.get() / (double) index.get()) * 100)) + "%"));
return this;
}

// static

public static void encryptDirectory(String path, EncryptionFileProcessor processor) throws Exception {
encryptDirectory(path, processor, null, null);
}

public static void encryptDirectory(String path, EncryptionFileProcessor processor, Consumer<File> searchLog, Consumer<File> processorLog) throws Exception {
List<File> fileList = new LinkedList<>();
FileSearchStream.of(new File(path)).forEach(file -> {
if(searchLog != null) {
searchLog.accept(file);
}
fileList.add(file);
});
for (File file : fileList) {
if(processorLog != null) {
processorLog.accept(file);
}
processor.encryptFile(file);
}
}

public static void decryptDirectory(String path, EncryptionFileProcessor processor) throws Exception {
decryptDirectory(path, processor, null, null);
}

public static void decryptDirectory(String path, EncryptionFileProcessor processor, Consumer<File> searchLog, Consumer<File> processorLog) throws Exception {
List<File> fileList = new LinkedList<>();
FileSearchStream.of(new File(path)).forEach(file -> {
if(searchLog != null) {
searchLog.accept(file);
}
fileList.add(file);
});
for (File file : fileList) {
if(processorLog != null) {
processorLog.accept(file);
}
processor.decryptFile(file);
}
}

public static List<String> listDirectory(String path, EncryptionFileProcessor processor, boolean deep) throws Exception {
List<String> res = new LinkedList<>();
for (File file : FileSearchStream.of(new File(path)).setDeep(deep).toList(file -> file)) {
if(file.isDirectory()) {
res.add("directory [" + file.getName() + "]");
} else {
res.add("file [" + processor.decryptFileName(file.getName()) + "]");
}
}
return res;
}
}

测试

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
package com.colin.tool;

import com.colin.tool.file.EncryptionDirectoryProcessor;

/**
*
* @author colin.cheng
* @date 2021-12-02
* @since 1.0.0
*/
public class Test {

public static void main(String[] args) throws Exception {

// 设置密码及默认打印日志
final EncryptionDirectoryProcessor processor = new EncryptionDirectoryProcessor("12333");

// 可选设置

// 使用内置的打印方法,也可以单独设置
//processor.defaultLog();
// 是否保存源文件,默认不保存
//processor.setSaveResource(true);
// 处理后的文件存放位置,默认是源文件所在文件夹
//processor.setToPath("D:\\test\\store");
// 也可以链式调用
// processor = new EncryptionDirectoryProcessor("12333").defaultLog().setSaveResource(true).setToPath("D:\\test\\store");

// 加密
processor.encryptDirectory("D:\\test");
// 查看加密文件原始信息,deep表示遍历子目录
processor.listDirectory("D:\\test", true).forEach(System.out::println);
// 解密
processor.decryptDirectory("D:\\test");
}
}

常用方法

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
package com.colin.tool.file;

/**
* @author colin.cheng
* @date
* @since 1.0.0
*/
public class EncryptionUtil {

/** 密码测试 */
public static String testPwd(String path, String... pwds) {
for (int i = 0; i < pwds.length; i++) {
try {
EncryptionDirectoryProcessor processor = new EncryptionDirectoryProcessor(pwds[i]);
processor.listDirectory(path,false);
return pwds[i];
} catch (Exception e) {}
}
return null;
}

public static void show(String path, String pwd) throws Exception {
// 设置密码及默认打印日志
final EncryptionDirectoryProcessor processor = new EncryptionDirectoryProcessor(pwd);
processor.listDirectory(path, true).forEach(System.out::println);
}

public static void encryptDirectory(String path, String pwd) throws Exception {
final EncryptionDirectoryProcessor processor = new EncryptionDirectoryProcessor(pwd);
processor.encryptDirectory(path);
}

public static void decryptDirectory(String path, String pwd) throws Exception {
final EncryptionDirectoryProcessor processor = new EncryptionDirectoryProcessor(pwd);
processor.decryptDirectory(path);
}
}