【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("成功生成新的密匙");
        }
    }
}
原文地址:https://www.cnblogs.com/xkxf/p/7107140.html