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

📄 main.java

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

import javax.swing.*;
import hartech.ui.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import hartech.JDirectory;

/**
 * <p>Title: J Secure Kit </p>
 *
 * <p>Description:
 *
 * functions:
 * 1, encrypt/decrypt one file or files in the directory using DES algorithms
 * 2, get the message digest of one file or files in the directory using SHA
 *
 * </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>
 */
public class Main {

  public static void main(String arg[]) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        //J.setLookAndFeel("Metal");
        new UI();
      }
    });
  }

  /**
   * get the message digest of the input file or all files in the directory
   *
   * @param file File
   * @return byte[]   160-bit SHA message digest
   */
  public static byte[] getMessageDigest(File file) {
    try {
      MessageDigest sha = MessageDigest.getInstance("SHA1");
      byte[] buffer = new byte[4096];
      FileInputStream in;

      JDirectory jDir = new JDirectory(file);
      File fileIn = jDir.nextFile();
      while (fileIn != null) {
        in = new FileInputStream(fileIn);

        // process this file
        while (in.read(buffer) != -1) {
          sha.update(buffer);
        }

        in.close();
        // get the next file
        fileIn = jDir.nextFile();
      }

      return sha.digest();
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
  }

  /**
   * convert byte array of hash value into Hex string for showing in UI
   *
   * @param bytes byte[]
   * @return String
   */
  public static String getHexString(byte[] bytes) {
    StringBuffer sb = new StringBuffer();
    String temp;
    int v;
    for (int i = 0; i < bytes.length; i++) {
      // byte's int value is -128 ~ 127
      // trun it to 0 ~ 255 for convert to Hex string
      v = (int) bytes[i];
      if (v < 0) {
        v += 256;
      }
      temp = Integer.toHexString(v);
      if (temp.length() == 1) {
        temp = "0" + temp;
      }
      sb.append(temp + " ");
    }
    return sb.toString().trim();
  }

  /**
   * encrypt and decrypt
   *
   * @param keyStr String  not less than 7-byte
   *                       if more than 7 bytes, only former 7 bytes make sense
   * @param fin File
   * @param fout File
   * @param isEncrypt boolean   true to encrypt, false to decrypt
   * @return boolean            success or not
   */
  public static boolean enDeCrypt(String keyStr, File fin, File fout,
                                  boolean isEncrypt) {
    try {
      SecretKey key = new SecretKeySpec(addParity(keyStr.getBytes()), "DES");

      FileInputStream in = new FileInputStream(fin);
      Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
      if (isEncrypt) {
        desCipher.init(Cipher.ENCRYPT_MODE, key);
      }
      else {
        desCipher.init(Cipher.DECRYPT_MODE, key);
      }
      FileOutputStream out = new FileOutputStream(fout);
      CipherOutputStream cos = new CipherOutputStream(out, desCipher);
      byte[] enBuffer = new byte[4096];
      int n;
      while ( (n = in.read(enBuffer)) != -1) {
        cos.write(enBuffer, 0, n);
      }
      cos.close();
      out.close();
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * Covert a 56-bit key to 64-bit which contain 8-bit parity bits
   *        64-bit key is required in DES
   *
   * from internet:
   *       http://www.javaref.cn/egs/javax.crypto/MakeDes.html
   *
   * The DES encrypter/decrypter requires a 64-bit key where only 56-bit
   * are significant. The other 8-bit are parity bits used to ensure that
   * the key has not been corrupted. To make the 64-bit key, the 56-bit value
   * is broken up into 7-bit chunks. Each 7-bit chunk is moved into an 8-bit
   * slot taking up the most significant bit positions. The least significant
   * bit (the parity bit) is set to either 1 or 0 in order to make the quantity
   * of 1 bits in the byte an odd number.
   * This example implements a method to convert a 56-bit value into a valid
   * DES key.
   *
   *
   * Such a method could be used to convert a 7-character string password
   * to a valid DES key.
   *
   *
   * Takes a 7-byte quantity and returns a valid 8-byte DES key.
   * The input and output bytes are big-endian, where the most significant
   * byte is in element 0.
   *
   * @param in byte[]  not less than 7 bytes,
   *                   if more than 7 bytes, only former 7 bytes make sense
   * @return byte[]    64-bit
   */
  public static byte[] addParity(byte[] in) {
    byte[] result = new byte[8];

    // Keeps track of the bit position in the result
    int resultIx = 1;

    // Used to keep track of the number of 1 bits in each 7-bit chunk
    int bitCount = 0;

    // Process each of the 56 bits
    for (int i = 0; i < 56; i++) {
      // Get the bit at bit position i
      boolean bit = (in[6 - i / 8] & (1 << (i % 8))) > 0;

      // If set, set the corresponding bit in the result
      if (bit) {
        result[7 - resultIx / 8] |= (1 << (resultIx % 8)) & 0xFF;
        bitCount++;
      }

      // Set the parity bit after every 7 bits
      if ( (i + 1) % 7 == 0) {
        if (bitCount % 2 == 0) {
          // Set low-order bit (parity bit) if bit count is even
          result[7 - resultIx / 8] |= 1;
        }
        resultIx++;
        bitCount = 0;
      }
      resultIx++;
    }
    return result;
  }

}

⌨️ 快捷键说明

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