A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© songyongpeng 中级黑马   /  2016-2-27 07:55  /  1060 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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) {
                                                        int tempb;
                                                        if ((tempb = bis.read()) != -1) {
                                                                b = b ^ tempb;
                                                                tempb = tempb ^ key;
                                                                bos.write(b);
                                                                bos.write(tempb);
                                                        } else {
                                                                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) {
                                                        int tempb;
                                                        if ((tempb = bis.read()) != -1) {
                                                                tempb = tempb ^ key;
                                                                b = b ^ tempb;
                                                                bos.write(b);
                                                                bos.write(tempb);
                                                        } else {
                                                                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("");
        }
}


2 个回复

倒序浏览
huang_bing_hua 来自手机 中级黑马 2016-2-27 08:19:42
沙发
好复杂,真牛……………………
回复 使用道具 举报
这是基础班的东西吗
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马