com.kaso.fileutils
--DirScanner.java
--FileParser.java
--FileParserException.java
com.kaso.encryption.view;
--EncryptionFrame.java
com.kaso.encryption.service;
--Encryption.java
--EncryptionParser.java
--DecryptionParser.java
com.kaso.encryption.test;
--MainApp.java
01 package com.kaso.fileutils; 02 03 import java.io.*; 04 05 public class DirScanner { 06 public static void scanning(File baseDir, FileParser fp) throws RuntimeException { 07 if(!baseDir.isDirectory()) { 08 throw new RuntimeException("不是目录"); 09 } 10 fp.start(baseDir); 11 try { 12 scanning(baseDir, baseDir, fp); 13 } catch(FileParserException e) { 14 e.printStackTrace(); 15 } 16 fp.end(baseDir); 17 } 18 19 private static void scanning(File baseDir, File file, FileParser fp) throws FileParserException { 20 if(file.isDirectory()) { 21 fp.startDir(baseDir, file); 22 File[] files = file.listFiles(); 23 if(files != null) { 24 for(File f : files) { 25 scanning(baseDir, f, fp); 26 } 27 } 28 } else { 29 fp.startFile(baseDir, file); 30 } 31 } 32 } |
FileParser.java:
01 package com.kaso.fileutils; 02 03 import java.io.*; 04 05 public interface FileParser { 06 void start(File baseDir); 07 08 void end(File baseDir); 09 10 void startDir(File baseDir, File dir) 11 throws FileParserException; 12 13 void startFile(File baseDir, File file) 14 throws FileParserException; 15 } |
FileParserException.java
01 package com.kaso.fileutils; 02 03 import java.io.*; 04 05 public class FileParserException extends IOException { 06 public FileParserException() { 07 super(); 08 } 09 10 public FileParserException(Throwable t) { 11 super(t); 12 } 13 14 public FileParserException(String s) { 15 super(s); 16 } 17 18 public FileParserException(String s, Throwable t) { 19 super(s, t); 20 } 21 } |
EncryptionFrame.java
001 package com.kaso.encryption.view; 002 003 import java.awt.GridLayout; 004 import java.awt.HeadlessException; 005 import java.awt.event.ActionEvent; 006 import java.awt.event.ActionListener; 007 import java.io.File; 008 009 import javax.swing.JButton; 010 import javax.swing.JCheckBox; 011 import javax.swing.JFileChooser; 012 import javax.swing.JFrame; 013 import javax.swing.JLabel; 014 import javax.swing.JOptionPane; 015 import javax.swing.JPanel; 016 import javax.swing.JPasswordField; 017 import javax.swing.JTextField; 018 019 import com.kaso.encryption.service.Encryption; 020 021 public class EncryptionFrame extends JFrame { 022 private JTextField filePath = new JTextField(15); // 目录路径文本框 023 private JPasswordField pwd = new JPasswordField(10); // 密码框 024 private JButton chooseBtn = new JButton("选择目录"); // 选择目录按钮 025 private JButton confirmBtn = new JButton("加密/解密"); // 确认按钮 026 private JCheckBox decrypt = new JCheckBox("解密"); 027 private JLabel fileLabel = new JLabel("选择目录:"); // 选择目录标签 028 private JLabel pwdLabel = new JLabel("输入密码:"); // 密码标签 029 private JPanel filePanel = new JPanel(); // 选择目录面板 030 private JPanel pwdPanel = new JPanel(); // 密码面板 031 private JFileChooser jfc = new JFileChooser(new File(".")); 032 private File file; 033 034 public EncryptionFrame() throws HeadlessException { 035 this.init(); 036 this.addComponent(); 037 this.addListener(); 038 } 039 040 private void init() { 041 this.setLayout(new GridLayout(2, 1)); 042 this.setSize(500, 140); 043 this.setLocation(400, 300); 044 this.setTitle("文件加密/解密器"); 045 } 046 047 private void addComponent() { 048 filePanel.add(fileLabel); 049 filePanel.add(filePath); 050 filePanel.add(chooseBtn); 051 052 pwdPanel.add(pwdLabel); 053 pwdPanel.add(pwd); 054 pwdPanel.add(decrypt); 055 pwdPanel.add(confirmBtn); 056 057 this.add(filePanel); 058 this.add(pwdPanel); 059 060 jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置可选中文件类型 061 } 062 063 private void addListener() { 064 chooseBtn.addActionListener(new ActionListener() { 065 public void actionPerformed(ActionEvent e) { 066 int result = jfc.showOpenDialog(EncryptionFrame.this); // 打开文件选择器 067 if (result == JFileChooser.APPROVE_OPTION) { // 确定操作 068 file = jfc.getSelectedFile(); 069 if (!file.isDirectory()) { 070 JOptionPane 071 .showMessageDialog(EncryptionFrame.this, 072 "请选择目录!", "警告", 073 JOptionPane.INFORMATION_MESSAGE); 074 return; 075 } 076 filePath.setText(file.getAbsolutePath()); // 路径赋给文本框 077 } 078 } 079 }); 080 081 confirmBtn.addActionListener(new ActionListener() { 082 public void actionPerformed(ActionEvent e) { 083 if (filePath.getText().equals("")) { // 判断是否选择文件 084 JOptionPane.showMessageDialog(EncryptionFrame.this, 085 "请选择文件!", "提示", JOptionPane.INFORMATION_MESSAGE); 086 return; 087 } 088 if(pwd.getPassword().length == 0) { 089 JOptionPane.showMessageDialog(EncryptionFrame.this, 090 "请输入密码!", "提示", JOptionPane.INFORMATION_MESSAGE); 091 return; 092 } 093 if (decrypt.isSelected()) { // 判断解密框是否选中 094 Encryption.decrypt(EncryptionFrame.this.file, pwd 095 .getPassword()); // 调用解密方法 096 JOptionPane.showMessageDialog(EncryptionFrame.this, 097 "解密完成!", "解密", JOptionPane.INFORMATION_MESSAGE); 098 } else { 099 Encryption.encrypt(EncryptionFrame.this.file, pwd 100 .getPassword());// 调用加密方法 101 JOptionPane.showMessageDialog(EncryptionFrame.this, 102 "加密完成!", "加密", JOptionPane.INFORMATION_MESSAGE); 103 } 104 } 105 }); 106 } 107 } |
DecryptionParser.java:
01 package com.kaso.encryption.service; 02 03 import java.io.File; 04 import java.io.FileInputStream; 05 import java.io.FileNotFoundException; 06 import java.io.FileOutputStream; 07 import java.io.IOException; 08 import java.io.InputStream; 09 import java.io.OutputStream; 10 11 import com.kaso.fileutils.FileParser; 12 import com.kaso.fileutils.FileParserException; 13 14 public class DecryptionParser implements FileParser { 15 private char[] key; 16 17 public DecryptionParser(char[] key) { 18 this.key = key; 19 } 20 21 public void end(File baseDir) { 22 } 23 24 public void start(File baseDir) { 25 } 26 27 public void startDir(File baseDir, File dir) throws FileParserException { 28 } 29 30 public void startFile(File baseDir, File file) throws FileParserException { 31 InputStream in = null; 32 OutputStream out = null; 33 try { 34 in = new FileInputStream(file); 35 out = new FileOutputStream(file.getAbsolutePath().substring(0, 36 file.getAbsolutePath().length() - 5)); 37 byte[] buff = new byte[1024]; // 缓冲区 38 int n = -1; 39 while ((n = in.read(buff)) != -1) { 40 for (int i = 0; i < buff.length; i++) {// 对数组进行循环解密 41 buff[i] -= key[i % key.length]; 42 } 43 out.write(buff, 0, n); 44 out.flush(); 45 } 46 } catch (FileNotFoundException e) { 47 e.printStackTrace(); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } finally { 51 try { 52 in.close(); 53 out.close(); 54 file.delete(); // 删除源文件 55 } catch (IOException e) { 56 e.printStackTrace(); 57 } 58 } 59 } 60 } |
Encryption.java:
01 package com.kaso.encryption.service; 02 03 import java.io.File; 04 05 import com.kaso.fileutils.DirScanner; 06 07 public class Encryption { 08 09 public static void encrypt(File file, char[] key) { 10 DirScanner.scanning(file, new EncryptionParser(key)); 11 } 12 13 public static void decrypt(File file, char[] key) { 14 DirScanner.scanning(file, new DecryptionParser(key)); 15 } 16 } |
EncryptionParser.java:
01 package com.kaso.encryption.service; 02 03 import java.io.File; 04 import java.io.FileInputStream; 05 import java.io.FileNotFoundException; 06 import java.io.FileOutputStream; 07 import java.io.IOException; 08 import java.io.InputStream; 09 import java.io.OutputStream; 10 11 import com.kaso.fileutils.FileParser; 12 import com.kaso.fileutils.FileParserException; 13 14 public class EncryptionParser implements FileParser { 15 private char[] key; 16 17 public EncryptionParser(char[] key) { 18 this.key = key; 19 } 20 21 public void end(File baseDir) { 22 } 23 24 public void start(File baseDir) { 25 } 26 27 public void startDir(File baseDir, File dir) throws FileParserException { 28 } 29 30 public void startFile(File baseDir, File file) throws FileParserException { 31 InputStream in = null; 32 OutputStream out = null; 33 try { 34 in = new FileInputStream(file); 35 out = new FileOutputStream(file.getAbsolutePath() + ".Kaso!"); 36 byte[] buff = new byte[1024]; // 缓冲区 37 int n = -1; 38 while ((n = in.read(buff)) != -1) { 39 for (int i = 0; i < buff.length; i++) {// 对数组进行循环加密 40 buff[i] += key[i % key.length]; 41 } 42 out.write(buff, 0, n); 43 } 44 } catch (FileNotFoundException e) { 45 e.printStackTrace(); 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } finally { 49 try { 50 in.close(); 51 out.close(); 52 file.delete(); // 删除源文件 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 } 58 } |
MainApp.java:
01 package com.kaso.encryption.test; 02 03 import javax.swing.JFrame; 04 05 import com.kaso.encryption.view.EncryptionFrame; 06 07 public class MainApp { 08 public static void main(String[] args) { 09 EncryptionFrame ef = new EncryptionFrame(); 10 ef.pack(); 11 ef.setVisible(true); 12 ef.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 13 14 } 15 } |