⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jfilecryptframe.java

📁 很强的JAVA加密、解密算法源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.sourceforge.jfilecrypt;//Download to http://www.codefans.net//~--- JDK imports ------------------------------------------------------------import java.awt.*;import java.awt.event.*;import java.io.*;import java.security.*;import java.util.Enumeration;import java.util.Properties;import java.util.Vector;import java.util.zip.*;import javax.crypto.*;import javax.crypto.spec.*;import javax.swing.*;import javax.swing.event.*;//~--- classes ----------------------------------------------------------------public class jFileCryptFrame extends JFrame {    private static final long serialVersionUID = 1L;        private JButton        btChoose = new JButton();    private JProgressBar   pbCryptProgress = new JProgressBar();    private JPasswordField pfPassword = new JPasswordField();    private JLabel         lbAlgorithm = new JLabel();    private JLabel         lbSource = new JLabel();    private JTextField     tfSourceFile = new JTextField();    private JLabel         lbPassword = new JLabel();    private JComboBox      cmbAlgorithm = new JComboBox();    private JComboBox      cmbCompressionLevel = new JComboBox();    private JButton        btDecrypt = new JButton();    private JButton        btEncrypt = new JButton();    private JFileChooser   fchooser = new JFileChooser();    private JCheckBox      chbUseCompression = new JCheckBox();        private Properties prop = new Properties();    private long read = 0;    private int start_for_entry_path = 0;    private String dir_for_encrypted ="";    private long size_all_files = 0;        //~--- constructors -------------------------------------------------------    /**     * This is the main-constructor. It calls the @see jbinit() method.     */        public jFileCryptFrame() {        super();        try {            jbInit();        } catch (Exception e) {            e.printStackTrace();        }    }    //~--- methods ------------------------------------------------------------    private void decrypt(final File f) {        if(f.isDirectory()) {            File[] files = f.listFiles();            for(int i = 0; i<files.length; i++) {                decrypt(files[i]);            }        } else {        new Thread() {            public void run() {                try {                    String kind = (String) cmbAlgorithm.getSelectedItem(); // Which algorithm?                    int index = kind.indexOf("(");                    kind = kind.substring(0, index);                    Cipher c = Cipher.getInstance(kind);                    Key k = new SecretKeySpec(                        new String(pfPassword.getPassword()).getBytes(), kind);                                        c.init(Cipher.DECRYPT_MODE, k);                    String filename = f.getCanonicalPath();                    if(filename.endsWith(prop.getProperty(kind))) {                        filename = filename.substring(                            0, filename.length()                            - prop.getProperty(kind).length());    // -suffix                    } else {                      displayError("Error: Wrong file chosen",                          "Ending of file and chosen algorithm do not match! Filename must end with: " + prop.getProperty(kind));                      return;                    }                    FileInputStream fis =                        new FileInputStream(f.getCanonicalPath());                    FileOutputStream fos = new FileOutputStream(filename);                    CipherInputStream cis = new CipherInputStream(fis, c);                    byte[] buffer = new byte[0xFFFF];                    final long size = f.length();                    pbCryptProgress.setMaximum((int) size);                    for (int len; (len = cis.read(buffer)) != -1;) {                        fos.write(buffer, 0, len);                        read += len;                        SwingUtilities.invokeLater(new Runnable() {                            public void run() {                                pbCryptProgress.setValue((int)read);                                pbCryptProgress.repaint();                            }                        });    // Set Progress                    }                    cis.close();                    fos.flush();                    fos.close();                    fis.close();                    pbCryptProgress.setMaximum(100);                    pbCryptProgress.setValue(0);                    read = 0;                } catch (Exception x) {                    x.printStackTrace();                }            }        }.start();        }    }    private void encrypt(final File f) {        if(f.isDirectory()) {            File[] files = f.listFiles();            for(int i = 0; i<files.length; i++) {                encrypt(files[i]);            }        } else {        new Thread() {            public void run() {                try {                    String kind = (String) cmbAlgorithm.getSelectedItem(); // Which algorithm?                    int index = kind.indexOf("(");                    kind = kind.substring(0, index);                    Cipher c = Cipher.getInstance(kind);                    Key k = new SecretKeySpec(                        new String(pfPassword.getPassword()).getBytes(), kind);                    c.init(Cipher.ENCRYPT_MODE, k);                    FileInputStream fis =                        new FileInputStream(f.getCanonicalPath());                    FileOutputStream fos =                        new FileOutputStream(f.getCanonicalPath()                                             + prop.getProperty(kind));                    CipherOutputStream cos = new CipherOutputStream(fos, c);                    final int size = (int) f.length();                    byte[] buffer = new byte[0xFFFF];                    pbCryptProgress.setMaximum(size);                    for (int len; (len = fis.read(buffer)) != -1; ) {                        cos.write(buffer, 0, len);                        read += len;                        SwingUtilities.invokeLater(new Runnable() {                            public void run() {                                pbCryptProgress.setValue((int)read);                                pbCryptProgress.repaint();                            }                        });    // Set Progress                    }                    cos.flush();                    cos.close();                    fos.flush();                    fos.close();                    fis.close();                    pbCryptProgress.setMaximum(100);                    pbCryptProgress.setValue(0);                                        read = 0;                } catch (Exception x) {                    x.printStackTrace();                }            }        }.start();        }    }        /**     * This method shows a @see javax.swing.JFileChooser and calls then      * the method @see setFileChosen(boolean b).     * @param e     */        private void btChoose_actionPerformed(ActionEvent e) {        try {            if (fchooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {                String path = fchooser.getSelectedFile().getCanonicalPath();                tfSourceFile.setText(path);            }        } catch (IOException ioex) {        }    }        /**     * This method is called by a click of btEncrypt. It calls     * the @see zip_encrypt(final file f, ZipOutputstream zos) or @see encrypt(final File f).     * @param e     */        private void btEncrypt_actionPerformed(ActionEvent e) {        String path = tfSourceFile.getText();        if(! path.equals("")) { // File chosen?            File file = new File(path);            if(file.exists()) { // Does file exist?                if(chbUseCompression.isSelected()) {                     String root_directory = "";                    if(file.isFile()) {                        root_directory = file.getParent();                    } else{                        root_directory = file.getPath();                    }                    start_for_entry_path = root_directory.length();                    zipVectorEncrypt(new File(path));                } else {                    encrypt(new File(path));                }            } else {                displayError("Error: file does not exist",                    "The file you have chosen does not exist: " + path);            }        }    }        /**     * This method is called by a click of btDecrypt. It calls     * the @see zip_decrypt(final file f) or @see decrypt(final File f).     * @param e     */        private void btDecrypt_actionPerformed(ActionEvent e) {        String path = tfSourceFile.getText();        if(! path.equals("")) { // File chosen?            File file = new File(path);            if(file.exists()) { // Does file exist?                if(chbUseCompression.isSelected()) {                    String fname = file.getName();                    // check if the suffix of selected file is ".zip"                    if(!fname.endsWith("zip")) {                      displayError("Error: Not a ZIP-Archive",                          "The chosen file is not a ZIP-Archive!");                      return;                    }                    try {                        if(!zip_Has_Only_One_File(file)){                            File pf = file.getParentFile();                            File decrypt_dir = new File(pf,fname.substring(0,fname.length()-4));                            decrypt_dir.mkdir();                            dir_for_encrypted = decrypt_dir.getPath();                        }else{                            File pf = file.getParentFile();                            dir_for_encrypted = pf.getPath();                                                 }                        size_all_files = file.length();                        pbCryptProgress.setMaximum((int) size_all_files);                        zip_decrypt(new ZipInputStream(new FileInputStream(path)));                    } catch (FileNotFoundException f) {}                    catch(ZipException zipex){                        displayError("Error: Not a valid ZIP-Archive",                            "The chosen file is not a valid ZIP-Archive!");                    }                    catch(IOException ioex){}                } else {                    decrypt(new File(path));                }            } else {                displayError("Error: file does not exist",                    "The file you have chosen does not exist: " + path);            }        }    }        /**     * This method initialises the GUI.     * @throws Exception     */         private void jbInit() throws Exception {        this.setTitle("jFileCrypt 0.1.4");        this.setSize(new Dimension(500, 250));        this.setResizable(true);                // setup components                btChoose.setText("Choose File");        btChoose.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                btChoose_actionPerformed(e);            }        });                btEncrypt.setText("Encrypt");        btEncrypt.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                btEncrypt_actionPerformed(e);            }        });                chbUseCompression.setText("Use ZIP-Compression");        chbUseCompression.addChangeListener(new ChangeListener() {                                                public void stateChanged(ChangeEvent e) {                                                    chbUseCompression_stateChanged(e);                                                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -