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

📄 actions.java

📁 用图形用户界面编写的加解密文件系统
💻 JAVA
字号:
package hartech.kids.jSecureKit;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import hartech.*;

/**
 * <p>Title: J Secure Kit </p>
 *
 * <p>Description:
 *
 * all the buttons' actions
 *
 * </p>
 *
 * <p>Website: www.hartech.cn </p>
 * <p>Page: http://www.hartech.cn/blog/blogview.asp?logID=92 </p>
 * <p>Date: 2006-12-26 </p>
 */


// hash button action
class Action_hash
    extends AbstractAction {
  public Action_hash() {
    super("Get Digest");
    putValue(MNEMONIC_KEY, KeyEvent.VK_G);
  }

  public void actionPerformed(ActionEvent e) {
    File fileSrc = new File(UI.jTextField_fileSrc.getText());
    if (!fileSrc.exists()) {
      JOptionPane.showMessageDialog(UI.jFrame,
                                    "Please choose source file/dir first!" +
                                    "\r\nOr the file/dir you input " +
                                    "is not existed!"
                                    , "Not source file/dir found!",
                                    JOptionPane.WARNING_MESSAGE);
      return;
    }

    long time = System.currentTimeMillis();

    byte[] result = Main.getMessageDigest(fileSrc);

    // update UI
    UI.jTextField_hash.setText(Main.getHexString(result));

    // show process time and whether save result to file
    time = System.currentTimeMillis() - time;
    int v = JOptionPane.showConfirmDialog(UI.jFrame,
                                          "Process is done!" +
                                          "\r\nUsed time:   " +
                                          ( (time / 3600000) % 60) + " : " +
                                          ( (time / 60000) % 60) + " : " +
                                          ( (time / 1000) % 60) + "." +
                                          ( (time / 100) % 10) +
                                          "\r\n\r\nSave the result to file?"
                                          , "Save to file?",
                                          JOptionPane.YES_NO_OPTION,
                                          JOptionPane.QUESTION_MESSAGE);

    // save result to file
    if (v == 0) {
      int returnVal = UI.jFileChooser_fileDest.showSaveDialog(UI.jFrame);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File fileDest = UI.jFileChooser_fileDest.getSelectedFile();
        BufferedOutputStream out = null;
        try {
          out = new BufferedOutputStream(new FileOutputStream(fileDest));
          out.write(result);
          out.flush();
          out.close();
        }
        catch (Exception ex) {
          ex.printStackTrace();
          JOptionPane.showMessageDialog(UI.jFrame,
                                        "The file you chose can't be written!"
                                        , "Wrong destination file!",
                                        JOptionPane.WARNING_MESSAGE);
          return;
        }
        JOptionPane.showMessageDialog(UI.jFrame,
                                      "Save to file successlly!"
                                      , "File saved!",
                                      JOptionPane.INFORMATION_MESSAGE);
      }
    }
  }
}

// file chooser button
class JActionListener
    implements ActionListener {
  public void actionPerformed(ActionEvent e) {

    // show file chooser for src file
    if (e.getActionCommand().equals("fileSrc")) {

      int returnVal = UI.jFileChooser_fileSrc.showOpenDialog(UI.jFrame);

      if (returnVal == JFileChooser.APPROVE_OPTION) {
        UI.jTextField_fileSrc.setText(UI.jFileChooser_fileSrc
                                      .getSelectedFile().getAbsoluteFile().
                                      toString());
      }
    }

    // show file chooser for dest file
    else if (e.getActionCommand().equals("fileDest")) {
      File fileSrc = new File(UI.jTextField_fileSrc.getText());
      if (fileSrc != null) {
        UI.jFileChooser_fileDest.setSelectedFile(new File(fileSrc
            .getName()));
      }
      int returnVal = UI.jFileChooser_fileDest.showSaveDialog(UI.jFrame);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        UI.jTextField_fileDest.setText(UI.jFileChooser_fileDest.
                                       getSelectedFile().getAbsoluteFile().
                                       toString());
      }
    }

    // encrypt or decrypt
    else if (e.getActionCommand().equals("encrypt") ||
             e.getActionCommand().equals("decrypt")) {

      File fileSrc = new File(UI.jTextField_fileSrc.getText());
      File fileDest = new File(UI.jTextField_fileDest.getText());
      String key = UI.jTextField_key.getText();

      boolean isEncrypt;
      if (e.getActionCommand().equals("encrypt")) {
        isEncrypt = true;
      }
      else {
        isEncrypt = false;
      }

      if (!checkInput(fileSrc, fileDest)) {
        return;
      }

      long time = System.currentTimeMillis();

      // source is a file
      if (fileSrc.isFile()) {
        Main.enDeCrypt(key, fileSrc, fileDest, isEncrypt);
      }

      // source is a directory
      else {
        JDirectorySaveAs jDir = new JDirectorySaveAs(fileSrc, fileDest);
        FileBag fileBag = jDir.nextBag();
        while (fileBag != null) {
          Main.enDeCrypt(key, fileBag.fileSrc, fileBag.fileDest, isEncrypt);
          fileBag = jDir.nextBag();
        }
      }

      time = System.currentTimeMillis() - time;
      JOptionPane.showMessageDialog(UI.jFrame,
                                    "Process is done!" +
                                    "\r\nUsed time:   " +
                                    ( (time / 3600000) % 60) + " : " +
                                    ( (time / 60000) % 60) + " : " +
                                    ( (time / 1000) % 60) + "." +
                                    ( (time / 100) % 10)
                                    , "Finished!",
                                    JOptionPane.INFORMATION_MESSAGE);
    }
  }

  // check whether the src/dest file/dir and key is formated
  // used before encrypt and decrypt
  boolean checkInput(File fileSrc, File fileDest) {

    // source file/dir not existed
    if (!fileSrc.exists()) {
      JOptionPane.showMessageDialog(UI.jFrame,
                                    "Please choose source file/dir first!" +
                                    "\r\nOr the file/dir you input " +
                                    "is not existed!"
                                    , "Not source file/dir found!",
                                    JOptionPane.WARNING_MESSAGE);
      return false;
    }

    // dest file/dir not input
    if (UI.jTextField_fileDest.getText().equals("")) {
      JOptionPane.showMessageDialog(UI.jFrame,
                                    "Please choose destination file/dir first!"
                                    , "Not destination file/dir found!",
                                    JOptionPane.WARNING_MESSAGE);
      return false;
    }

    // key is less than 7 bytes
    if (UI.jTextField_key.getText().length() < 7) {
      JOptionPane.showMessageDialog(UI.jFrame,
                                    "Key must not less than 7 bytes!"
                                    , "Key is not standard!",
                                    JOptionPane.WARNING_MESSAGE);
      return false;
    }
    return true;
  }
}

⌨️ 快捷键说明

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