【Java】Swing+IO流實現一個簡單的檔案加密程式

來源:互聯網
上載者:User

標籤:alt   decrypt   頁面   frame   加密檔案   location   title   jbutton   jframe   

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("成功產生新的密匙");        }    }}

 

【Java】Swing+IO流實現一個簡單的檔案加密程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.