mirror of
https://gitee.com/czh-dev/upload-hub
synced 2026-09-06 16:07:07 +08:00
feat:实现文件加解密
This commit is contained in:
+32
-7
@@ -6,22 +6,23 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RequestMapping("/localFile")
|
||||
@RestController
|
||||
public class LocalSearchController {
|
||||
public class LocalFileController {
|
||||
|
||||
@Resource
|
||||
private ILocalFileService fileSearchService;
|
||||
private ILocalFileService localFileService;
|
||||
|
||||
/**
|
||||
* 获取系统磁盘列表
|
||||
*/
|
||||
@GetMapping("/getDrives")
|
||||
public Result<?> getDrives() {
|
||||
return Result.success(fileSearchService.getDrives());
|
||||
return Result.success(localFileService.getDrives());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,7 +30,7 @@ public class LocalSearchController {
|
||||
*/
|
||||
@PostMapping("/buildIndex")
|
||||
public Result<?> buildIndex(@RequestBody List<String> drives) {
|
||||
return Result.success(fileSearchService.buildIndex(drives));
|
||||
return Result.success(localFileService.buildIndex(drives));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,7 +38,7 @@ public class LocalSearchController {
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public Result<?> search(@RequestParam String keyword) {
|
||||
return Result.success(fileSearchService.searchFiles(keyword));
|
||||
return Result.success(localFileService.searchFiles(keyword));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,10 +67,34 @@ public class LocalSearchController {
|
||||
|
||||
) {
|
||||
// 避免直接输入磁盘进去全盘扫描
|
||||
if (path.length() == 2 && path.endsWith(":")) {
|
||||
if (path.length() == 3 && path.endsWith(":\\") && maxDepth != 1) {
|
||||
return Result.error("请选择目录");
|
||||
}
|
||||
|
||||
return Result.success(fileSearchService.getFileTree(path, showFiles, showFolders, maxDepth));
|
||||
return Result.success(localFileService.getFileTree(path, showFiles, showFolders, maxDepth));
|
||||
}
|
||||
|
||||
@PostMapping("/encrypt")
|
||||
public Result<?> encryptFile(@RequestParam String filePath, @RequestParam String password) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
return Result.error("文件不存在");
|
||||
}
|
||||
if (file.isDirectory()) {
|
||||
return Result.error("不能加密目录,请选择文件");
|
||||
}
|
||||
return Result.success(localFileService.encryptFile(filePath, password));
|
||||
}
|
||||
|
||||
@PostMapping("/decrypt")
|
||||
public Result<?> decryptFile(@RequestParam String filePath, @RequestParam String password) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
return Result.error("文件不存在");
|
||||
}
|
||||
if (file.isDirectory()) {
|
||||
return Result.error("不能解密目录,请选择文件");
|
||||
}
|
||||
return Result.success(localFileService.decryptFile(filePath, password));
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package cn.czh.service;
|
||||
import cn.czh.dto.FileNode;
|
||||
import cn.czh.dto.IndexResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public interface ILocalFileService {
|
||||
@@ -15,4 +16,7 @@ public interface ILocalFileService {
|
||||
|
||||
FileNode getFileTree(String path, boolean showFiles, boolean showFolders, int maxDepth);
|
||||
|
||||
String encryptFile(String filePath, String password);
|
||||
|
||||
String decryptFile(String filePath, String password);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.czh.service.impl;
|
||||
|
||||
import cn.czh.base.BusinessException;
|
||||
import cn.czh.dto.FileNode;
|
||||
import cn.czh.dto.IndexResult;
|
||||
import cn.czh.service.ILocalFileService;
|
||||
@@ -9,7 +10,16 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -20,6 +30,13 @@ public class LocalFileServiceImpl implements ILocalFileService {
|
||||
@Resource
|
||||
private FileSearchUtil fileSearchUtil;
|
||||
|
||||
private static final String ALGORITHM = "AES";
|
||||
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
|
||||
private static final int KEY_LENGTH = 256;
|
||||
private static final int ITERATION_COUNT = 65536;
|
||||
private static final int SALT_LENGTH = 16;
|
||||
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
log.info("关闭 FileSearchUtil 资源...");
|
||||
@@ -117,4 +134,126 @@ public class LocalFileServiceImpl implements ILocalFileService {
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encryptFile(String filePath, String password) {
|
||||
Path path = Paths.get(filePath);
|
||||
if (!Files.exists(path)) {
|
||||
throw new BusinessException("文件未找到: " + filePath);
|
||||
}
|
||||
|
||||
String encryptedFilePath = filePath + ".encrypted";
|
||||
Path encryptedPath = Paths.get(encryptedFilePath);
|
||||
if (Files.exists(encryptedPath)) {
|
||||
throw new BusinessException("加密文件已存在: " + encryptedFilePath);
|
||||
}
|
||||
|
||||
try {
|
||||
byte[] salt = new byte[SALT_LENGTH];
|
||||
new SecureRandom().nextBytes(salt);
|
||||
|
||||
SecretKey key = generateKey(password, salt);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||
|
||||
try (InputStream in = Files.newInputStream(path);
|
||||
OutputStream out = Files.newOutputStream(encryptedPath)) {
|
||||
|
||||
out.write(salt);
|
||||
|
||||
byte[] iv = cipher.getIV();
|
||||
out.write(iv);
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
byte[] output = cipher.update(buffer, 0, bytesRead);
|
||||
if (output != null) {
|
||||
out.write(output);
|
||||
}
|
||||
}
|
||||
byte[] outputBytes = cipher.doFinal();
|
||||
if (outputBytes != null) {
|
||||
out.write(outputBytes);
|
||||
}
|
||||
}
|
||||
return encryptedFilePath;
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
Files.deleteIfExists(encryptedPath);
|
||||
} catch (IOException ex) {
|
||||
log.error("删除加密失败的文件时出错: {}", ex.getMessage(), ex);
|
||||
}
|
||||
throw new BusinessException("文件加密失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String decryptFile(String filePath, String password) {
|
||||
Path path = Paths.get(filePath);
|
||||
if (!Files.exists(path)) {
|
||||
throw new BusinessException("文件未找到: " + filePath);
|
||||
}
|
||||
|
||||
String decryptedFilePath = filePath.replace(".encrypted", "");
|
||||
Path decryptedPath = Paths.get(decryptedFilePath);
|
||||
if (Files.exists(decryptedPath)) {
|
||||
throw new BusinessException("解密文件已存在: " + decryptedFilePath);
|
||||
}
|
||||
|
||||
try {
|
||||
// Read salt and IV
|
||||
byte[] salt = new byte[SALT_LENGTH];
|
||||
byte[] iv = new byte[16];
|
||||
try (InputStream in = Files.newInputStream(path)) {
|
||||
if (in.read(salt) != SALT_LENGTH) {
|
||||
throw new BusinessException("加密文件格式无效");
|
||||
}
|
||||
if (in.read(iv) != 16) {
|
||||
throw new BusinessException("加密文件格式无效");
|
||||
}
|
||||
}
|
||||
|
||||
SecretKey key = generateKey(password, salt);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, new javax.crypto.spec.IvParameterSpec(iv));
|
||||
|
||||
try (InputStream in = Files.newInputStream(path);
|
||||
OutputStream out = Files.newOutputStream(decryptedPath)) {
|
||||
|
||||
in.skip(SALT_LENGTH + 16);
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
byte[] output = cipher.update(buffer, 0, bytesRead);
|
||||
if (output != null) {
|
||||
out.write(output);
|
||||
}
|
||||
}
|
||||
byte[] outputBytes = cipher.doFinal();
|
||||
if (outputBytes != null) {
|
||||
out.write(outputBytes);
|
||||
}
|
||||
}
|
||||
return decryptedFilePath;
|
||||
} catch (Exception e) {
|
||||
// 删除已生成的解密文件
|
||||
try {
|
||||
Files.deleteIfExists(decryptedPath);
|
||||
} catch (IOException ex) {
|
||||
log.error("删除解密失败的文件时出错: {}", ex.getMessage(), ex);
|
||||
}
|
||||
throw new BusinessException("文件解密失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
private SecretKey generateKey(String password, byte[] salt) throws Exception {
|
||||
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
|
||||
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
||||
byte[] keyBytes = factory.generateSecret(keySpec).getEncoded();
|
||||
return new SecretKeySpec(keyBytes, ALGORITHM);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user