Zxy97Encryption 加密程序

版权声明:[email protected] https://blog.csdn.net/zhaoxuyang1997/article/details/81714090

Zxy97Encryption 加密程序 ,支持国际化语言(中/英),使用方法:
java -jar jar包路径 <文件输入路径> <文件输出路径> <数字密码,0表示不加密>
Zxy97Encryption 加密程序


控制台输出:

D:\Code\java\Zxy97Encryption\dist>java -jar Zxy97Encryption.jar README.TXT README2.txt 2
文件加密程序([email protected] | 2018-08-15 23:44)
文件加密输入路径:D:\Code\java\Zxy97Encryption\dist\README.TXT
文件加密输出路径:D:\Code\java\Zxy97Encryption\dist\README2.txt
正在加密中,请稍后……
加密完成,用时:0.016s
文件已加密为:D:\Code\java\Zxy97Encryption\dist\README2.txt

D:\Code\java\Zxy97Encryption\dist>java -jar Zxy97Encryption.jar README2.TXT README3.txt 2
文件加密程序([email protected] | 2018-08-15 23:44)
文件加密输入路径:D:\Code\java\Zxy97Encryption\dist\README2.TXT
文件加密输出路径:D:\Code\java\Zxy97Encryption\dist\README3.txt
正在加密中,请稍后……
加密完成,用时:0.016s
文件已加密为:D:\Code\java\Zxy97Encryption\dist\README3.txt

D:\Code\java\Zxy97Encryption\dist>java -jar Zxy97Encryption.jar README2.TXT README4.txt 1
文件加密程序([email protected] | 2018-08-15 23:44)
文件加密输入路径:D:\Code\java\Zxy97Encryption\dist\README2.TXT
文件加密输出路径:D:\Code\java\Zxy97Encryption\dist\README4.txt
正在加密中,请稍后……
加密完成,用时:0.016s
文件已加密为:D:\Code\java\Zxy97Encryption\dist\README4.txt

Zxy97Encryption.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Zxy97Encryption {
     static final Locale myLocale = Locale.getDefault();

    private static final String[] zh_CN = {
        "文件加密程序([email protected] | 2018-08-15 23:44)",
        "未输入数字密码!",
        "文件未加密,已退出程序。",
        "未输入加密的文件的输入路径,",
        "未输入加密的文件的输出路径,",
        "文件不存在!",
        "文件加密输入路径:",
        "文件加密输出路径:",
        "已存在,是否覆盖?",
        "已覆盖文件:",
        "文件已加密为:",
        "正在加密中,请稍后……",
        "加密完成,用时:",
        "java -jar jar包路径 <文件输入路径> <文件输出路径> <数字密码,0表示不加密>"
    };
    private static final String[] en_US = {
        "File encryption procedures ([email protected] |  2018-08-15 23:44)",
        "No numeric password entered!" ,
        "The file is unencrypted and is out of the program." ,
        "Input path of unencrypted file, ",
        "Output path of unencrypted file, ",
        "File does not exist!" ,
        "File encryption input path: ",
        "File encrypted output path: ",
        "Is it covered?" ,
        "Overwritten file: ",
        "The document has been encrypted as: ",
        "Encrypted, please wait ...",
        "Encryption complete, time: ",
        "Java -jar JAR_PATH < FILE_INPUT_PATH > < FILE_OUTPUT_PATH > < NUMERIC_PASSWORD, 0 means unencrypted >"
    };

    static String [] strs = "zh_CN".equals(myLocale.toString()) ? zh_CN : en_US;

    public static void main(String[] args) {
        System.out.println(strs[0]);

        String srcPath;
        String destPath;
        int n;

        try{            
            if(isInteger(args[2])){
                n = Integer.valueOf(args[2]);
            }else{
                n = -1;
                System.out.println(strs[1] + strs[2]);
                System.exit(n);
            }

            srcPath=args[0];
            if(isArgEmpty(srcPath)){
                System.out.println(strs[3] + strs[2]);
                System.exit(1);
            }
            File fileSrc = new File(srcPath);
            if(!fileSrc.exists()){
                System.out.println(fileSrc.getAbsolutePath() + strs[5]);
                System.exit(1);
            }
            System.out.println(strs[6] + fileSrc.getAbsolutePath());

            destPath=args[1];
            if(isArgEmpty(destPath)){
                System.out.println(strs[4] + strs[2]);
                System.exit(1);
            }
            File fileDest = new File(destPath);
            System.out.println(strs[7] + fileDest.getAbsolutePath());
            if(fileDest.exists()){
                System.out.println(strs[7] + fileDest.getAbsolutePath()+ strs[8] + "(Y/N)");
                Scanner input = new Scanner(System.in);
                String yOrN = input.nextLine();
                if("Y".equals(yOrN) || "y".equals(yOrN)){
                    System.out.println(strs[9] + fileDest.getAbsolutePath());
                }else{
                    System.out.println(strs[2]);
                    System.exit(0);
                }  
            }

            try (FileOutputStream fos = new FileOutputStream(destPath)) {                
                try (FileInputStream fis = new FileInputStream(fileSrc)) {
                    Date dateStart = new Date();
                    System.out.println(strs[11]);
                    cypher(fis,fos,n);
                    Date dateEnd = new Date();
                    System.out.println(strs[12] + (double)(dateEnd.getTime()- dateStart.getTime())/1000 + "s"); 
                    System.out.println(strs[10] + fileDest.getAbsolutePath());
                    fis.close();
                }
                fos.flush();
            }
        }catch(NumberFormatException | IOException | ArrayIndexOutOfBoundsException e){
            System.out.printf("%s\n%s\n", strs[13],"java -jar Zxy97Encryption.jar D:/1.txt d:/2.txt 1234");
        }
    }

    private static boolean isInteger(String str) {  
         Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
    }    
    private static boolean isArgEmpty(String arg){
        return arg == null || "".equals(arg);
    }
    private static void cypher(InputStream ips,OutputStream ops,int n) throws IOException{   
        int b;
        while((b = ips.read())!= -1){
            ops.write(b ^ n);
        }     
    }
}

猜你喜欢

转载自blog.csdn.net/zhaoxuyang1997/article/details/81714090