这款小程序是可以根据用户的输入对硬盘中指定目录下的文件进行加密。用户如果对文件进行重复加密则会提示未操作,而文件也将不再次做加密;如果用户对为加密的内容进行解密,程序也将提示未操作,而文件也将不进行解密操作;如果用户在解密过程中输入的密钥是错误的,那么将无法进行解密操作,程序也会提示未操作。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Crazy {

    private static Scanner sc = new Scanner(System.in);
    private static String filePath = "";
    private static int key = 0;
    private static String bombom = ""; //,
    private static byte[] keyArr;
    private static String basicLock = "";
    private static String basicKey = "91,80,111,119,101,114,32,66,121,32,76,105,72,117,97,110,93,";

    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        while (true) {
            println("****************************************************");
            println("*--------------------------------------------------*");
            println("*--------接下来发生的事情有可能出乎你的意料--------*");
            println("*---------------------准备开始---------------------*");
            println("*--------------------------------------------------*");
            println("****************************************************");
            println("");
            String tempSC = scanner("请输入要操作的路径:");
            filePath = tempSC;
            File file = getFile(filePath);
            if (file.exists()) {
                if (file.isDirectory()) {
                    boolean isEncrypt;
                    while (true) {
                        println("****************************************************");
                        println("*--------------------------------------------------*");
                        tempSC = scanner("*------------你要进行什么操作(加密/解密)-----------*");
                        if ("jiami".equals(tempSC) || "加密".equals(tempSC)) {
                            isEncrypt = true;
                            break;
                        } else if ("jiemi".equals(tempSC) || "解密".equals(tempSC)) {
                            isEncrypt = false;
                            break;
                        } else {
                            //println("*********************请重新输入*********************");
                        }
                    }
                    println("");
                    println("****************************************************");
                    println("*--------------------------------------------------*");
                    tempSC = scanner("*-----------------请输入KEY(密钥)------------------*");
                    bombom = "";
                    keyArr = tempSC.getBytes();
                    key = 0;
                    for (int i = 0; i < keyArr.length; i++) {
                        bombom += ((256 - Math.abs(keyArr[i])) ^ 1) + ",";
                        key += (256 - Math.abs(keyArr[i]));
                    }
                    key = Math.abs(key);
                    if (isEncrypt) {
                        encrypt(file);
                    } else {
                        decrypt(file);
                    }
                } else {
                    //println("你输入的是文件路径!");
                }
            } else {
                println("路径不存在!");
            }
            println("----------------------------------------------------");
            println("****************************************************");
            println("");
        }
    }

    public static boolean checkFile(BufferedInputStream bis,String checkType) throws IOException {
        String sss = "";
        String[] sArr = bombom.split(",");
        basicLock = "";
        int b;
        for (int i = 0; i < basicKey.split(",").length; i++) {
            if ((b = bis.read()) != -1) {
                basicLock += (b + ",");
            } else {
                //break;
            }
        }
        if ("encrypt".equals(checkType)) {
            if (basicKey.equals(basicLock)) {
                //基础密钥一致,说明是已经经过加密的,因此返回false跳过,不再加密处理
                return false;
            } else {
                //基础密钥不一致
                //进行加密
                return true;
            }
        } else if ("decrypt".equals(checkType)) {
            if (basicKey.equals(basicLock)) {
                //基础密钥一致
                int forNum = bis.read();
                if (forNum == -1) {
                    return false; //跳出
                } else {
                    for (int j = 0; j < forNum; j++) {
                        if ((b = bis.read()) != -1) {
                            sss += (b + ",");
                        } else {
                            break;
                        }
                    }
                    if (sss.equals(bombom)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } else {
                //基础密钥不一致,说明文件没有经过加密,因此无需进行加密处理,直接返回false跳过
                return false;
            }
        } else {
            println("未知错误!");
            return false;
        }
    }

    public static void encrypt(File file) throws NumberFormatException, IOException {
        File[] fileLists = getFileLists(file);
        if (fileLists != null) {
            for (File f : fileLists) {
                if (f.isDirectory()) {
                encrypt(f);
                } else {
                    FileInputStream fis = new FileInputStream(f.getPath());
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    FileOutputStream fos = new FileOutputStream(f.getPath() + ".lihuan");
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    if (checkFile(bis, "encrypt")) {
                        bis.close();
                        int b;
                        String[] sArr = basicKey.split(",");
                        for (String s : sArr) {
                            bos.write(Integer.parseInt(s));
                        }
                        bos.write(keyArr.length);
                        sArr = bombom.split(",");
                        for (String s : sArr) {
                            bos.write(Integer.parseInt(s));
                        }
                        fis = new FileInputStream(f.getPath());
                        bis = new BufferedInputStream(fis);
                        while ((b = bis.read()) != -1) {
                            b = b ^ key;
                            bos.write(b);
                        }
                        bos.close();
                        bis.close();
                        File fileCopy = new File(f.getPath() + ".lihuan");
                        String filePath = f.getPath();
                        System.out.println(f.getPath() + "[加密成功]");
                        f.delete();
                        File fileNew = new File(filePath);
                        fileCopy.renameTo(fileNew);
                    } else {
                        System.out.println(f.getPath() + "[未操作]");
                        bos.close();
                        bis.close();
                        File fileCopy = new File(f.getPath() + ".lihuan");
                        fileCopy.delete();
                    }
                }
            }
        }
    }

    public static void decrypt(File file) throws NumberFormatException, IOException {
        File[] fileLists = getFileLists(file);
        if (fileLists != null) {
            for (File f : fileLists) {
                if (f.isDirectory()) {
                    decrypt(f);
                } else {
                    FileInputStream fis = new FileInputStream(f.getPath());
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    FileOutputStream fos = new FileOutputStream(f.getPath() + ".lihuan");
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    if (checkFile(bis, "decrypt")) {
                        int b;
                        while ((b = bis.read()) != -1) {
                            b = b ^ key;
                            bos.write(b);
                        }
                        bos.close();
                        bis.close();
                        File fileCopy = new File(f.getPath() + ".lihuan");
                        String filePath = f.getPath();
                        println(f.getPath() + "[解密成功]");
                        f.delete();
                        File fileNew = new File(filePath);
                        fileCopy.renameTo(fileNew);
                    } else {
                        println(f.getPath() + "[未操作]");
                        bos.close();
                        bis.close();
                        File fileCopy = new File(f.getPath() + ".lihuan");
                        fileCopy.delete();
                    }
                }
            }
        }
    }

    public static File getFile(String filePath) {
        File file = new File(filePath);
        return file;
    }

    public static File[] getFileLists(File file) {
        File[] fileLists = file.listFiles();
        return fileLists;
    }

    public static String scanner(String message) {
        println(message);
        String tempStr = sc.nextLine();
        if ("exit".equals(tempStr.toLowerCase()) || "tuichu".equals(tempStr.toLowerCase()) || "退出".equals(tempStr.toLowerCase())) {
            System.out.println("程序正在退出......");
            System.exit(0);
        } else {

        }
        return tempStr;
    }

    public static void print(String str) {
        System.out.print(str);
    }

    public static void println(String str) {
        System.out.println(str);
        print("");
    }
}

如果程序有任何BUG,欢迎反馈。


发表评论

电子邮件地址不会被公开。 必填项已用*标注