黑马程序员技术交流社区

标题: 自己做的小案例:对文件的加密解密,要求加密后文件路径不变 [打印本页]

作者: jxsryqt    时间: 2017-2-6 15:09
标题: 自己做的小案例:对文件的加密解密,要求加密后文件路径不变
package com.yqt.xc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Secret {


作者: jxsryqt    时间: 2017-2-6 15:13
郁闷了,后面没显示...
作者: jxsryqt    时间: 2017-2-6 15:21
内容在这,jar文件上传不了,只能用txt的了

加解密案例.txt

3.33 KB, 下载次数: 27


作者: jxsryqt    时间: 2017-2-6 15:25
下载还要黑马币,好吧,内容是这样的:
public class Secret {

        /**
         * 案例描述:对文件加密及解密
         *                 要求:对文件进行加密,要求加密后文件路径不变,将加密密码写入到其他文件中.
         *                         解密时,手动输入密码,密码正确解密       
         *
         * 分析步骤:
         *
         * 对文件加密,密码自行输入
                 * 1,获取需要加密的文件
                 * 2,提示输入密码,并将密码存储到相应密码文件中
                 * 3,开始加密
         *
         * 解密
                 * 4,手动输入密码
                 * 5,读取密码文件
                 * 6,核对输入的密码与文件的密码是否一致
                 * 7,一致则解密
作者: jxsryqt    时间: 2017-2-6 15:26
public static void main(String[] args) throws IOException {
                demo1();        //加密
                makeOpen();        //解密
        }
作者: jxsryqt    时间: 2017-2-6 15:27
private static void demo1() throws IOException {
                File file = getFile();
                int key = setKey(file);
                secrect(file, key);
                System.out.println("文件加密成功");
        }
//1,获取需要加密的文件
        public static File getFile() {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入文件路径");
                while (true) {
                        String s = sc.nextLine();
                        File file = new File(s);        //包装成File对象
                        if (!file.exists()) {
                                System.out.println("文件不存在,请重新输入");
                        }else if (file.isDirectory()) {
                                System.out.println("输入的是文件夹路径,请重新输入");
                        }else {
                                return file;
                        }
                }
        }
作者: jxsryqt    时间: 2017-2-6 15:28
//2,提示输入密码,并将密码存储到密码文件中
        public static int setKey(File file) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请设置加密密码");
                while(true) {
                        String s = sc.nextLine();
                        try {
                                int key = Integer.parseInt(s);
                                BufferedWriter bw = new BufferedWriter(new FileWriter(file.getName() + "key.txt"));
                                bw.write(s);       
                                bw.close();
                                return key;
                        } catch (Exception e) {
                                System.out.println("密码格式错误,只能为纯数字");
                        }
                }
        }
作者: jxsryqt    时间: 2017-2-6 15:29
//3,开始加密
        public static void secrect(File file,int key) throws IOException {
                File temp = new File("temp");
                try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)) ;
                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(temp)) ;
                ) {
                        int len;
                        while ((len = bis.read()) != -1) {
                                bos.write(len ^ key);                //读取原文件,写出时异或
                        }
                } finally {
                        String name = file.getName();                //获取原文件名称
                        file.delete();                                                //删除原文件
                        temp.renameTo(new File(name));                //将加密后的文件名改为原文件名
                }
        }
作者: jxsryqt    时间: 2017-2-6 15:30
//解密
        public static void makeOpen() {
                File file = getFile();        //获取需要解密文件               
                //4,手动输入密码
                Scanner sc = new Scanner(System.in);       
                System.out.println("请输入密码");
                while (true) {
                        String s = sc.nextLine();
                        try {
                                int value = Integer.parseInt(s);        //获取输入的密码                               
                //5,关联密码文件获取密码
                                try {
                                        BufferedReader br = new BufferedReader(new FileReader(file.getName() + "key.txt"));       
                                        /*
                                         * 读取的密码文件存在时进行后续操作
                                         * 读取的密码文件不存在时,跳转至catch,提示该文件不是加密文件
                                         */
                                        int key = Integer.parseInt(br.readLine());
                                       
                //6,校验密码是否正确
                                        if (key != value) {               
                                                System.out.println("密码错误,请重新输入");
                                        }else {
                //7,密码正确,开始解密.对同一个数异或两次得到原文件
                                                secrect(file, key);
                                                System.out.println("文件解密成功");
                                                break;
                                        }
                                } catch (Exception e) {
                                        System.out.println("该文件未被加密,请重新输入需要解密的文件");                         
                                }
                        } catch (Exception e) {
                                System.out.println("密码格式错误,只能为纯数字");
                        }
                }       
        }
}
作者: jxsryqt    时间: 2017-2-6 15:35
{:8_517:},搞定,好累.

这里有一点,文件加密后改为同一路径时,如果直接用流程编写,不加finally时,经常出错,导致出现两个文件,原文件和加密文件.小伙伴们可以测试下:下面是不加finally的
//3,开始加密
        public static void secrect(File file,int key) throws IOException {
                File temp = new File("temp");
                try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)) ;
                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(temp)) ;
                ) {
                        int len;
                        while ((len = bis.read()) != -1) {
                                bos.write(len ^ key);                //读取原文件,写出时异或
                                String name = file.getName();                //获取原文件名称
                                file.delete();                                                //删除原文件
                                temp.renameTo(new File(name));                //将加密后的文件名改为原文件名
                        }
                }
        }




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2