package com.qdmmy6; 
 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
import qdmmy6.file.*; 
import qdmmy6.file.listener.*; 
import java.io.*; 
 
/** 
 * 小心使用,使用不当文件无法还原!切记!切记!!! 
 * @author  
 * 
 */ 
public class JiaMiFrame extends JFrame { 
        public static void main(String[] args) { 
                JiaMiFrame frame = new JiaMiFrame(); 
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                frame.setVisible(true); 
        } 
         
        private JPasswordField pwdField = new JPasswordField(10);//密钥,用于加密与解密 
        private JButton btn = new JButton("Submit");                        //提交加密或解密操作 
         
        private FileOptions fo;                //用于扫描文件夹 
         
        private String password = "qdmmy6";        //默认密钥 
         
        private JCheckBox box = new JCheckBox("加密");                //操作,如果选中,表示加密操作,否则解密操作 
         
        private JTextField pathField = new JTextField(10);        //显示要加密或解密的文件夹 
        private JFileChooser jfc = new JFileChooser();                //胜于选择被加密或解密的文件夹 
        private JButton button = new JButton("Path");                //点击它会弹出JFileChooser 
         
        private JLabel label = new JLabel(new ImageIcon("1.gif"));//等待图片 
         
        public JiaMiFrame() { 
                this.init(); 
                this.addCommponent(); 
                this.addListener(); 
        } 
         
        private void init() { 
                this.setSize(262, 200); 
                this.setLocation(300, 200); 
                this.setResizable(false); 
        } 
         
        private void addCommponent() { 
                // 只能选择文件夹 
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
                // 文本框不能使用 
                pathField.setEditable(false); 
                 
                //该面板添加显示加密或解密路径的文本框,以及弹出JFileChooser的按钮 
                JPanel pathPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
                pathPanel.add(pathField); 
                pathPanel.add(button); 
                 
                //该面板添加密码框,以及提交加密或解密的按钮,还有选择操作项的JCheckBox(选中,表示加密操作,否则解密操作) 
                JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
                panel.add(pwdField); 
                panel.add(btn); 
                panel.add(box); 
                 
                this.add(pathPanel, BorderLayout.NORTH); 
                this.add(panel, BorderLayout.SOUTH); 
                 
                //等待图片在JLabel中居中 
                this.label.setHorizontalAlignment(JLabel.CENTER); 
                //默认不显示等待图片,只有在提交了加密或解密操作之后才显示,到操作完成后,再次隐藏等待图片 
                label.setVisible(false); 
                this.add(label); 
        }  
         
        private void addListener() { 
                btn.addActionListener(new ActionListener() { 
                        public void actionPerformed(ActionEvent evt) { 
                                //获取密码,如果用户没有输入密码,使用默认密码 
                                String pwd = new String(pwdField.getPassword()); 
                                password = pwd.length() > 0 ? pwd : password; 
                                //使用路径实例化fo,并给fo添加操作监听器,然后清空密码框中的文本 
                                fo = new FileOptions(pathField.getText()); 
                                fo.addFileSearchListener(new EncryptListener()); 
                                pwdField.setText(""); 
                                // 使用一个线程来完成扫描文件夹操作 
                                new Thread() { 
                                        public void run() { 
                                                fo.search(); 
                                        } 
                                }.start(); 
                                //使提交加密或解密操作的按钮不可用。 
                                btn.setEnabled(false); 
                                //使等待图片显示出来 
                                label.setVisible(true); 
                        } 
                }); 
                 
                button.addActionListener(new ActionListener() { 
                        public void actionPerformed(ActionEvent evt) { 
                                //弹出JFileChooser,获取用户选择的路径,并显示在文本框内 
                                int m = jfc.showOpenDialog(JiaMiFrame.this); 
                                if(m == JFileChooser.APPROVE_OPTION) { 
                                        pathField.setText(jfc.getSelectedFile().getAbsolutePath()); 
                                } 
                        } 
                }); 
        } 
         
        //加密解密算法,当flag为true时为加密,否则为解密 
        //将from复制到to。 
        //从from中获取的字节数组,先处理后,再写到to中。 
        //删除from文件 
        private static void encrypt(File from, File to, String k, boolean flag) { 
                try {   
                        BufferedInputStream in = new BufferedInputStream(new FileInputStream(from)); 
                        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to)); 
                        byte[] b = new byte[1024]; 
                        int i; 
                        while((i = in.read(b)) != -1) { 
                                encrypt(b, k, flag); 
                                out.write(b, 0, i); 
                        } 
                        in.close(); 
                        out.close(); 
                        from.delete(); 
                } catch(IOException exception) { 
                        exception.printStackTrace(); 
                } 
        } 
         
        //处理b 
        //flag为true时,每个字节减去相应的密钥,否则加。 
        //密码来自于key字符串中的字符。 
        //b[0]与b[key.getLength()]所使用的密钥都是key.getCharAt(0) 
        private static void encrypt(byte[] b, String key, boolean flag) { 
                int len = key.length(); 
                int index = 0; 
                for(int i = 0; i < b.length; i++) { 
                        if(flag) { 
                                b[i] -= (byte)key.charAt(index++ % len); 
                        } else { 
                                b[i] += (byte)key.charAt(index++ % len); 
                        } 
                } 
        } 
         
        //在FileOptions开始扫描文件夹时,会调用监听器的相应方法 
        private class EncryptListener extends AbstractListener { 
                 
                //当扫描到一个文件时,该方法会被调用 
                public void disposeFile(File file) { 
                        //if中完成的是加密,else中完成的是解密 
                        if(box.isSelected()) { 
                                //创建to文件,给原文件添加扩展名".Q6" 
                                File to = new File(file.getAbsolutePath() + ".Q6"); 
                                encrypt(file, to, password, true); 
                        } else { 
                                //把所有加密后的文件解密 
                                String path = file.getAbsolutePath(); 
                                //加密后的文件扩展名都为Q6,解密需要创建一个新文件,新文件的扩展名需要去除.Q6 
                                if(path.endsWith("Q6")) { 
                                        File to = new File(path.substring(0, path.lastIndexOf("."))); 
                                        encrypt(file, to, password, false); 
                                } 
                        } 
                         
                }         
                //扫描完成后会调用此方法 
                public void searchEnd() { 
                        box.setSelected(false); 
                        box.setEnabled(false); 
                        label.setVisible(false); 
                        btn.setEnabled(true); 
                        JOptionPane.showMessageDialog(JiaMiFrame.this, "成功!"); 
                } 
        } 
} 
 |   
        
 
    
    
    
     
 
 |