java md5

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 "解密成功";
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值