• 【Java】Swing+IO流实现一个简单的文件加密程序(demo版)


    留着参考

    EncrytService

    package com.my.service;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class EncryptService {
        // 默认密匙路径
        private static String DEFAULT_KEY_URL = ".//KEY";
        // 临时文件路径
        private static String DEFAULT_TEMP_URL = ".//TEMP";
        // 读取密匙
        private int key[] = new int[128];
        private void readKey() {
            File keyFile = new File(DEFAULT_KEY_URL);
            
            try {
                FileInputStream localKey = new FileInputStream(keyFile);
                for (int i = 0; i < 128; ++i) {
                    key[i] = localKey.read();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 生成新的密匙
        public void makeKey() {
            try {
                File keyFile = new File(DEFAULT_KEY_URL);
    
                FileOutputStream fos = new FileOutputStream(keyFile);
    
                for (int i = 0; i < 128; ++i) {
                    fos.write((int)(Math.random()*128));
                }
                readKey();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 加密文件
        public void encryptFile(File file) {
            try {
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(new File(DEFAULT_TEMP_URL));
    
                int length = fis.available();
                for (int i = 0; i < length; ++i) {
                    fos.write(fis.read() + key[i%128]);
                }
                fis.close();
                fos.close();
                FileInputStream fileInputStream = new FileInputStream(new File(DEFAULT_TEMP_URL));
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                int length2 = fileInputStream.available();
                for (int i = 0; i < length2; ++i) {
                    fileOutputStream.write(fileInputStream.read());
                }
                fileInputStream.close();
                fileOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 解密文件
        public void decryptFile(File file) {
            try {
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(new File(DEFAULT_TEMP_URL));
    
                int length = fis.available();
                for (int i = 0; i < length; ++i) {
                    fos.write(fis.read() - key[i%128]);
                }
                fis.close();
                fos.close();
                FileInputStream fileInputStream = new FileInputStream(new File(DEFAULT_TEMP_URL));
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                int length2 = fileInputStream.available();
                for (int i = 0; i < length2; ++i) {
                    fileOutputStream.write(fileInputStream.read());
                }
                fileInputStream.close();
                fileOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    Main

    package com.my.ui;
    
    import com.my.service.EncryptService;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    public class Main extends JFrame implements ActionListener {
        private EncryptService encryptService = new EncryptService();
        // 设置默认大小
        private static final int DEFAULT_WIDTH = 396;
        private static final int DEFAULT_HEIGHT = 145;
        // 组件
        private JFileChooser chooser;
        private JButton buttonEncrypt;
        private JButton buttonDecrypt;
        private JButton buttonMakeKey;
        JTextField fileText;
        JTextField keyText;
        // 文件路径
        private String filePath;
        private String keyPath;
        // 初始化加密页面
        public Main() {
            setTitle("文件加密程序");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            setResizable(false);
            setLocationRelativeTo(null);
    
            JPanel panUser = new JPanel();
            // 创建组件
            fileText = new JTextField();
            fileText.setEditable(false);
            keyText = new JTextField();
            keyText.setEditable(false);
            JButton btnFile = new JButton("....");
            btnFile.setFocusPainted(false);
            JButton btnKey = new JButton("...");
            btnKey.setFocusPainted(false);
            btnKey.setEnabled(false);
            // 布局
            panUser.setLayout(new GridLayout(2, 3));
            panUser.add(new JLabel("源文件路径:"));
            panUser.add(fileText);
            panUser.add(btnFile);
            panUser.add(new JLabel("密匙路径:"));
            panUser.add(keyText);
            panUser.add(btnKey);
    
            buttonEncrypt = new JButton("加密");
            buttonEncrypt.setFocusPainted(false);
            buttonDecrypt = new JButton("解密");
            buttonDecrypt.setFocusPainted(false);
            buttonMakeKey = new JButton("生成新的密匙");
            buttonMakeKey.setFocusPainted(false);
    
            JPanel panBtn = new JPanel();
            panBtn.setLayout(new FlowLayout());
            panBtn.add(buttonEncrypt);
            panBtn.add(buttonDecrypt);
            panBtn.add(buttonMakeKey);
    
            setLayout(new BorderLayout());
            add(panUser, BorderLayout.CENTER);
            add(panBtn, BorderLayout.SOUTH);
    
            // 注册事件监听
            btnFile.addActionListener(this);
            btnKey.addActionListener(this);
            buttonMakeKey.addActionListener(this);
            buttonEncrypt.addActionListener(this);
            buttonDecrypt.addActionListener(this);
    
            chooser = new JFileChooser();
            chooser.setCurrentDirectory(new File("."));
        }
    
        public static void main(String[] args) {
            JFrame frame = new Main();
            frame.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("....")) {
                int result = chooser.showOpenDialog(null);
                if (result == JFileChooser.APPROVE_OPTION) {
                    filePath = chooser.getSelectedFile().getPath();
                    fileText.setText(filePath);
                }
            }
            if (e.getActionCommand().equals("...")) {
                int result = chooser.showOpenDialog(null);
                if (result == JFileChooser.APPROVE_OPTION) {
                    keyPath = chooser.getSelectedFile().getPath();
                    keyText.setText(keyPath);
                }
            }
            if (e.getActionCommand().equals("加密")) {
                encryptService.encryptFile(new File(filePath));
                System.out.println("加密成功");
            }
            if (e.getActionCommand().equals("解密")) {
                encryptService.decryptFile(new File(filePath));
                System.out.println("解密成功");
            }
            if (e.getActionCommand().equals("生成新的密匙")) {
                encryptService.makeKey();
                keyText.setText(new File("").getAbsolutePath() + "\KEY");
                System.out.println("成功生成新的密匙");
            }
        }
    }
  • 相关阅读:
    JavaScript算法学习:获取字符串最后一位方法及判断是否以指定字符串开始或结尾
    前端基础知识学习笔记
    JavaScript算法练习:找出字符串中最长的单词并输出其长度
    HTML+CSS学习笔记
    Mint-ui中loadmore(上拉加载下拉刷新)组件在ios中滑动会触发点击事件的解决方法
    JavaScript中的DOM,BOM详细介绍;
    web前端面试题目整理
    Charles https乱码处理
    String类的三个参数的构造方法
    java.net包中的URL类
  • 原文地址:https://www.cnblogs.com/xkxf/p/7107140.html
Copyright © 2020-2023  润新知