import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import java.util.concurrent.*;
public class FileEncryptor {
private static byte[] combinedHash;
private static String inputFile;
private static int nowByte;
private static int count = 0;
public static void main(String[] args) throws NoSuchAlgorithmException {
Scanner scanner = new Scanner(System.in);
//获取文件路径
System.out.print("输入要加密或解密的文件名含后缀:");
inputFile = scanner.nextLine();
File file = new File(inputFile);
if (!file.exists()) {
System.out.println("请检查是否路径错误或不存在该文件!!!!");
return;
}
//输入密码
System.out.print("请输入要加密或解密的密码:");
String password = scanner.nextLine();
//加密文件位置,生成加密文件不是对源文件进行加密
System.out.print("输入要保存文件的路径位置(可不填):");
String outputPath = scanner.nextLine();
// Replace backslashes with forward slashes,替换掉路径保证jvm正常解析
inputFile = inputFile.replace("\\", "/");
outputPath = outputPath.replace("\\", "/");
//如果被加密过,获取文件路径
String outputFile;
if (inputFile.split("\\.")[1].endsWith("DATA")) {
String F = inputFile.split("\\.")[1].replace("DATA", "");
if (outputPath.isEmpty()) {
if (inputFile.contains("/")) {
outputFile = inputFile.split("/")[inputFile.split("/").length - 1].split("\\.")[0] + "." + F;
} else {
outputFile = inputFile.split("\\.")[0] + "." + F;
}
} else {
outputFile = outputPath + "/" + inputFile.split("/")[inputFile.split("/").length - 1].split("\\.")[0] + "." + F;
}
} else {
if (outputPath.isEmpty()) {
if (inputFile.contains("/")) {
outputFile = inputFile.split("/")[inputFile.split("/").length - 1].split("\\.")[0] + "." + inputFile.split("\\.")[1] + "DATA";
} else {
outputFile = inputFile.split("\\.")[0] + "." + inputFile.split("\\.")[1] + "DATA";
}
} else {
String extension = inputFile.split("\\.")[1];
outputFile = outputPath + "/" + inputFile.split("/")[inputFile.split("/").length - 1].split("\\.")[0] + "." + extension + "DATA";
}
}
//md5双重加密
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] passwordBytes = password.getBytes();
md.update(passwordBytes);
byte[] passwordHash = md.digest();
md.update(passwordHash);
byte[] passwordHash2 = md.digest();
combinedHash = new byte[passwordHash.length + passwordHash2.length];
// 将第一个哈希结果复制到combinedHash中
System.arraycopy(passwordHash, 0, combinedHash, 0, passwordHash.length);
// 将第二个哈希结果复制到combinedHash中
System.arraycopy(passwordHash2, 0, combinedHash, passwordHash.length, passwordHash2.length);
CountDownLatch countDownLatch = new CountDownLatch(10);
try {
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile);
ExecutorService poll = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
Thread n = new Thread(
() -> {
if (nowByte == -1) {
return;
} else {
int newByte = nowByte ^ combinedHash[count % combinedHash.length];
count++;
try {
out.write(newByte);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
countDownLatch.countDown();
}
);
poll.submit(n);
}
countDownLatch.await();
in.close();
out.close();
System.out.println("加密/解密完成。输出文件: " + outputFile);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//解密
String c = scanner.nextLine();
if (c.equalsIgnoreCase("y")) {
System.out.println("请输入密码:");
String b = scanner.nextLine();
System.out.print("输入要保存文件的路径位置:");
String v = scanner.nextLine();
Jiemi(b, v);
}
}
public static String Jiemi(String password, String outputFile1) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] passwordBytes = password.getBytes();
md.update(passwordBytes);
byte[] passwordHash = md.digest();
md.update(passwordHash);
byte[] passwordHash2 = md.digest();
byte[] combinedHash1 = new byte[passwordHash.length + passwordHash2.length];
System.arraycopy(passwordHash, 0, combinedHash1, 0, passwordHash.length);
System.arraycopy(passwordHash2, 0, combinedHash1, passwordHash.length, passwordHash2.length);
if (combinedHash1 == combinedHash) {
try {
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile1);
int newByte = 0;
while (true) {
int nowByte = in.read();
if (nowByte == -1) break;
out.write(newByte);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return "解密成功";
}
java md5
于 2023-09-22 15:50:25 首次发布