Java文件文件夹加密和解密

编程入门 行业动态 更新时间:2024-10-05 11:23:49

Java文件<a href=https://www.elefans.com/category/jswz/34/1449793.html style=文件夹加密和解密"/>

Java文件文件夹加密和解密

文件加密和解密

使用AES算法进行加密和解密的操作

AEC是高级加密标准(Advanced Encryption Standard)的缩写,是一种对称密钥加密算法,常用于数据加密和保护隐私。

package com.sin.utils;import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;/*** @createTime 2023/10/18 11:24* @createAuthor SIN* @use 文件的加密和解密*/
public class DocumentEncryptionUtil {// 文件的加密方式private static final String ALGORITHM = "AES";/*** 文件加密* @param secretKey  文件加密密钥* @param sourceFilePath  需要加密文件地址* @param destinationFilePath  加密后文件地址*/public static void encryptFile(String secretKey,String sourceFilePath, String destinationFilePath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {// 使用密钥字符串生成秘密密钥SecretKey secretKeySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);// 获取 AES 加密算法的实例Cipher cipher = Cipher.getInstance(ALGORITHM);// 使用秘密密钥初始化密码 cipher,设置为加密模式cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);// 获取源文件路径Path sourcePath = Paths.get(sourceFilePath);// 获取目标加密文件路径Path destinationPath = Paths.get(destinationFilePath);// 创建输入流,读取源文件try (InputStream inputStream = Files.newInputStream(sourcePath);// 创建输出流,写入加密文件OutputStream outputStream = Files.newOutputStream(destinationPath);// 创建密码输出流,连接到输出流,并使用密码 cipher 进行加密CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher)) {// 缓冲区大小byte[] buffer = new byte[4096];int bytesRead;// 读取源文件内容到缓冲区while ((bytesRead = inputStream.read(buffer)) != -1) {// 将加密后的数据写入加密文件cipherOutputStream.write(buffer, 0, bytesRead);}}}/*** 文件解密* @param secretKey 文件解密密钥* @param sourceFilePath 需要解密的文件地址* @param destinationFilePath 解密后的文件地址*/public static void decryptFile(String secretKey,String sourceFilePath, String destinationFilePath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {SecretKey secretKeySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM); // 使用密钥字符串生成秘密密钥Cipher cipher = Cipher.getInstance(ALGORITHM); // 获取 AES 加密算法的实例cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); // 使用秘密密钥初始化密码 cipher,设置为解密模式Path sourcePath = Paths.get(sourceFilePath); // 获取源加密文件路径Path destinationPath = Paths.get(destinationFilePath); // 获取目标解密文件路径try (InputStream inputStream = Files.newInputStream(sourcePath); // 创建输入流,读取加密文件OutputStream outputStream = Files.newOutputStream(destinationPath); // 创建输出流,写入解密文件CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher)) { // 创建密码输入流,连接到输入流,并使用密码 cipher 进行解密byte[] buffer = new byte[4096]; // 缓冲区大小int bytesRead;while ((bytesRead = cipherInputStream.read(buffer)) != -1) { // 读取加密文件内容到缓冲区outputStream.write(buffer, 0, bytesRead); // 将解密后的数据写入解密文件}}}}

文件加密

目标文件即内容

package com.sin.utils.test;import com.sin.utils.DocumentEncryptionUtil;
import org.junit.Test;import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;/*** @createTime 2023/10/18 11:28* @createAuthor SIN* @use*/
public class DocumentEncryptionUtilTest {/*** 文件加密*/@Testpublic void encryptFileTest(){try {DocumentEncryptionUtil.encryptFile("SIN-80238023-@@@","D:/FileEncryptionUtil.txt","D:/Demo/FileEncryptionUtil.txt");} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);} catch (NoSuchPaddingException e) {throw new RuntimeException(e);} catch (InvalidKeyException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}

加密后的文件

文件解密

 /*** 文件解密*/@Testpublic void decryptFileTest(){try {DocumentEncryptionUtil.decryptFile("SIN-80238023-@@@","D:/Demo/FileEncryptionUtil.txt","D:/Demo/FileEncryptionUtil1.txt");} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);} catch (NoSuchPaddingException e) {throw new RuntimeException(e);} catch (InvalidKeyException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}

解密后的文件

文件夹加密和解密

package com.sin.utils;import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;/*** @createTime 2023/10/19 9:20* @createAuthor SIN* @use 文件夹加密和解密*/
public class FileEncryptionUtil {//AES是高级加密标准(Advanced Encryption Standard)的缩写,是一种对称密钥加密算法,常用于数据加密和保护隐私。private static final String ALGORITHM = "AES";/*** 去除文件名扩展名* @param fileName 需要操作的文件* @return*/private static String removeExtension(String fileName) {// 找到文件的最后一个点int dotIndex = fileName.lastIndexOf(".");// 保证点不是文件名的第一个字符和最后一个字符if (dotIndex > 0 && dotIndex < fileName.length() - 1) {// 返回有效的扩展名return fileName.substring(0, dotIndex);}// 返回源文件return fileName;}/*** 文件夹加密* @param secretKey 文件夹加密密钥* @param sourceFilePath 需要加密的文件夹* @param destinationFilePath 加密后的文件夹地址*/public static void encryptFile(String secretKey,String sourceFilePath, String destinationFilePath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {// 使用密钥字符串生成秘密密钥SecretKey secretKeySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);// 获取 AES 加密算法的实例Cipher cipher = Cipher.getInstance(ALGORITHM);// 使用秘密密钥初始化密码 cipher,设置为加密模式cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);// 获取源文件或文件夹路径Path sourcePath = Paths.get(sourceFilePath);// 获取目标加密文件或文件夹路径Path destinationPath = Paths.get(destinationFilePath);if (Files.isDirectory(sourcePath) && !Files.exists(destinationPath)) {// 创建目标文件夹Files.createDirectories(destinationPath);// 遍历源文件夹try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(sourcePath)) {for (Path filePath : directoryStream) {// 加密后的文件名String encryptedFileName = filePath.getFileName().toString() + ".enc";// 加密后的文件路径String encryptedFilePath = destinationPath.resolve(encryptedFileName).toString();// 递归调用加密方法,处理子文件或子文件夹encryptFile(secretKey,filePath.toString(), encryptedFilePath);}}} else if (Files.isRegularFile(sourcePath)) {// 创建输入流,读取源文件try (InputStream inputStream = Files.newInputStream(sourcePath);// 创建输出流,写入加密文件OutputStream outputStream = Files.newOutputStream(destinationPath);// 创建密码输出流,连接到输出流,并使用密码 cipher 进行加密CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher)) {// 缓冲区大小byte[] buffer = new byte[4096];int bytesRead;// 读取源文件内容到缓冲区while ((bytesRead = inputStream.read(buffer)) != -1) {// 将加密后的数据写入加密文件cipherOutputStream.write(buffer, 0, bytesRead);}}}}/**** @param secretKey 文件夹解密密钥* @param sourceFilePath 需要解密的文件夹* @param destinationFilePath 解密后的文件夹地址*/public static void decryptFile(String secretKey,String sourceFilePath, String destinationFilePath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {SecretKey secretKeySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM); // 使用密钥字符串生成秘密密钥Cipher cipher = Cipher.getInstance(ALGORITHM); // 获取 AES 加密算法的实例cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); // 使用秘密密钥初始化密码 cipher,设置为解密模式Path sourcePath = Paths.get(sourceFilePath); // 获取源加密文件或文件夹路径Path destinationPath = Paths.get(destinationFilePath); // 获取目标解密文件或文件夹路径if (Files.isDirectory(sourcePath) && !Files.exists(destinationPath)) {Files.createDirectories(destinationPath); // 创建目标文件夹try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(sourcePath)) { // 遍历源文件夹for (Path filePath : directoryStream) {String decryptedFileName = removeExtension(filePath.getFileName().toString()); // 去除文件名的扩展名String decryptedFilePath = destinationPath.resolve(decryptedFileName).toString(); // 解密后的文件路径decryptFile(secretKey,filePath.toString(), decryptedFilePath); // 递归调用解密方法,处理子文件或子文件夹}}} else if (Files.isRegularFile(sourcePath)) {try (InputStream inputStream = Files.newInputStream(sourcePath); // 创建输入流,读取加密文件OutputStream outputStream = Files.newOutputStream(destinationPath); // 创建输出流,写入解密文件CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher)) { // 创建密码输入流,连接到输入流,并使用密码 cipher 进行解密byte[] buffer = new byte[4096]; // 缓冲区大小int bytesRead;while ((bytesRead = cipherInputStream.read(buffer)) != -1) { // 读取加密文件内容到缓冲区outputStream.write(buffer, 0, bytesRead); // 将解密后的数据写入解密文件}}}}}

需要加密的文件

加密文件

package com.sin.utils.test;import com.sin.utils.FileEncryptionUtil;
import org.junit.Test;import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;/*** @createTime 2023/10/19 9:32* @createAuthor SIN* @use 测试文件夹加密和解密*/
public class FileEncryptionUtilTest {/*** 加密文件*/@Testpublic void encryptFileTest(){try {FileEncryptionUtil.encryptFile("sin-80238023-@@@","D:/Demo","D:/Demo1");} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);} catch (NoSuchPaddingException e) {throw new RuntimeException(e);} catch (InvalidKeyException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}

加密后的文件

解密文件

/*** 解密文件*/
@Test
public void decryptFile(){try {FileEncryptionUtil.decryptFile("sin-80238023-@@@","D:/Demo1","D:/Demo2");} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);} catch (NoSuchPaddingException e) {throw new RuntimeException(e);} catch (InvalidKeyException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}
}

解密后的文件

更多推荐

Java文件文件夹加密和解密

本文发布于:2023-12-03 23:31:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1658966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹加密   文件   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!