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

📄 desjiajiemi.txt

📁 基于JAVA编程的DES算法加解密工厂的实现
💻 TXT
📖 第 1 页 / 共 4 页
字号:

package com.crypt.file; 

/** 
 * <p>Title: </p> 
 * 
 * <p>Description: 文件加解密实现算法工厂</p> 
 * 
 * <p>Copyright: Copyright (c) 2006</p> 
 * 
 * <p>Company: </p> 
 * 
 * @author not attributable 
 * @version 1.0 
 */ 
public class FileCryptFactory { 

    public static String KEY_DES = "KEY_DES";  //基于DES key 实现文件加解密 
     
    private static KeyDESFileCrypt keyDESFileCrypt =  new KeyDESFileCrypt(); 
    private FileCryptFactory() { 
    } 

    /** 
     * 根据不同的文件加解密算法,返回不同的操作实例 ,如果没有找到,返回 null 
     * @param type String 
     * @return KeyDESFileCrypt 
     */ 
    public static KeyDESFileCrypt create(String type){ 
        if(FileCryptFactory.KEY_DES.equals(type)){ 
            return keyDESFileCrypt; 
        } 
        return null; 
    } 

} 

package com.crypt.file; 

import javax.crypto.KeyGenerator; 
import java.security.NoSuchAlgorithmException; 
import java.security.Key; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.*; 
import javax.crypto.spec.SecretKeySpec; 
import javax.crypto.Cipher; 
import javax.crypto.*; 
import java.security.*; 
import java.util.Date; 
import javax.swing.JOptionPane; 

public class KeyDESFileCrypt { 


    public KeyDESFileCrypt(){ 
    } 

    public static void main(String[] args) throws IOException { 

//           String s = "1234"; 
//           byte []data = s.getBytes(); 
// 
//           Key key = generateDESKey(); 
//           data = cryptoDES(key,data); 
// 
//           data = deCryptoDES(key,data); 
// 
//           s = new String(data); 
// 


//        File keyFile = new File("f:\\key.key"); 
// 
//        Key key = generateDESKey(); 
// 
//        if(!saveDESKeyToFile(keyFile,key)){ 
//          System.out.println("保存key文件出错!"); 
//          if(keyFile.exists()){ 
//              keyFile.delete(); 
//          } 
//          return ; 
//      } 




//          File keyFile = new File("f:\\key.key"); 
//        File srcFile = new File("f:\\下半年计算机技术与软件专业技术资格考试.doc"); 
//    File destFile = new File("f:\\下半年计算机技术与软件专业技术资格考试.doc.data"); 
//    File dest2File = new File("f:\\下半年计算机技术与软件专业技术资格考试2.doc"); 
// 
// 
//    Date date = new Date(); 
//    testCryptoDESFile(keyFile, srcFile, destFile); 
//    testCryptoDESFile(keyFile, destFile, dest2File); 
//      //testDeCryptoDESFile(keyFile,destFile,dest2File); 
//     Date date2 = new Date(); 
//     String h = String.valueOf(date2.getHours()-date.getHours()); 
//     String m = String.valueOf(date2.getMinutes()-date.getMinutes()); 
//     String s = String.valueOf(date2.getSeconds()-date.getSeconds()); 
//     System.out.println("处理时间:"+h 
//                        +":"+m+":" 
//                        +s); 



    } 

//    private static void testDeCryptoDESFile(File keyFile, File srcFile, File destFile) throws 
//            IOException { 
//        if(!srcFile.exists()){ 
//            System.out.println("要解密的源文件不存在!"); 
//            return ; 
//        } 
// 
//        if(!destFile.exists()){ 
//            destFile.createNewFile(); 
//        } 
// 
//        if(!deCryptoDES(keyFile,srcFile,destFile)){ 
//            System.out.println("解密文件失败!"); 
//            return; 
//        } 
// 
//        System.out.println("成功解密文件"); 
//    } 

//    private static void testCryptoDESFile(File keyFile, File srcFile, File destFile) throws 
//            IOException { 
//        if(!srcFile.exists()){ 
//            System.out.println("要加密的源文件不存在!"); 
//            return ; 
//        } 
// 
//        if(!destFile.exists()){ 
//            destFile.createNewFile(); 
//        } 
// 
// 
//        if(!cryptoDES(keyFile,srcFile,destFile)){ 
//            System.out.println("加密文件失败!"); 
//            return; 
//        } 
// 
//        System.out.println("成功加密文件"); 
//    } 

    /** 
     * 使用DES加密文件 
     * @param keyFile File 包含key 数据的文件 
     * @param srcFile File 要加密的源文件 
     * @param destFile File 加密后的目标文件 
     * @return boolean 成功,返回 true ,否则 返回 false 
     */ 
    public  boolean cryptoDES(File keyFile, File srcFile, File destFile,DESFileThread thread) { 
        return translateDES(keyFile, srcFile, destFile, Cipher.ENCRYPT_MODE,thread); 
    } 

    /** 
     * 使用DES解密文件 
     * @param keyFile File 包含key 数据的文件 
     * @param srcFile File 要加密的源文件 
     * @param destFile File 加密后的目标文件 
     * @return boolean 成功,返回ture ,否则 返回 false 
     */ 
    public  boolean deCryptoDES(File keyFile, File srcFile, File destFile,DESFileThread thread) { 
       return translateDES(keyFile, srcFile, destFile, Cipher.DECRYPT_MODE,thread); 
    } 

    /** 
     * 使用DES加密字节数据 
     * @param key Key 对称key 
     * @param data byte[] 要加密的字节数据 
     * @return byte[] 成功,返回加密后的字节数据,否则,返回 null 
     */ 
    public  byte[] deCryptoDES(Key key, byte[] data) { 
        return translateDES(key, data, Cipher.DECRYPT_MODE); 
    } 

    /** 
     * 使用DES解密字节数据 
     * @param key Key 对称key 
     * @param data byte[] 要解密的字节数据 
     * @return byte[] 成功,返回解密后的字节数据,否则,返回 null 
     */ 
    public  byte[] cryptoDES(Key key, byte[] data) { 
        return translateDES(key, data, Cipher.ENCRYPT_MODE); 
    } 

    /** 
     * 从一个包含key 数据的文件中,获取 key 对称key 
     * @param file File 包含key 数据的文年 
     * @return Key 成功,返回获取到的key ,否则,返回 null 
     */ 
    public  Key getDESKeyFormFile(File file) { 
      FileInputStream fin = null; 
      int flen = -1; 
      byte[] keyByte = null; 
      SecretKeySpec keySpec = null; 
      try { 
          fin = new FileInputStream(file); 
      } catch (FileNotFoundException ex) { 
          ex.printStackTrace(); 
          return null; 
      } 
      try { 
          flen = fin.available(); 
          keyByte = new byte[flen]; 
          fin.read(keyByte); 
          keySpec = new SecretKeySpec(keyByte, "DES"); 
          return keySpec; 
      } catch (IOException ex1) { 
          ex1.printStackTrace(); 
          return null; 
      } 
  } 

  /** 
   * 将key 保存到文件中 
   * @param file File 将保存key的文件 
   * @param key Key 对称key 
   * @return boolean 成功,返回 ture ,否则,返回 false 
   */ 
  public  boolean saveDESKeyToFile(File file, Key key) { 
      byte[] keyByte = key.getEncoded(); 
      FileOutputStream fout = null; 
      try { 
          fout = new FileOutputStream(file); 
      } catch (FileNotFoundException ex) { 
          ex.printStackTrace(); 
          return false; 
      } 
      if (fout == null) { 
          return false; 
      } 
      try { 
          fout.write(keyByte); 
      } catch (IOException ex1) { 
          ex1.printStackTrace(); 
          return false; 
      } finally { 
          try { 
              fout.close(); 
          } catch (IOException ex2) { 
              ex2.printStackTrace(); 
              return false; 
          } 
      } 
      return true; 
  } 

  /** 
   * 产生一个对称key 
   * @return Key 
   */ 
  public  Key generateDESKey() { 
      KeyGenerator keyGen = null; 
      try { 
          keyGen = KeyGenerator.getInstance("DES"); 
      } catch (NoSuchAlgorithmException ex) { 
          ex.printStackTrace(); 
          return null; 
      } 
      keyGen.init(56); 
      return keyGen.generateKey(); 

  } 

  /** 
   * 转换文件编码 
   * @param keyFile File 
   * @param srcFile File 
   * @param destFile File 
   * @param mode int 
   * @return boolean 
   */ 
  private  boolean translateDES(File keyFile, File srcFile, File destFile, 
                                 int mode, DESFileThread thread) { 
        FileInputStream fin = null; 
        FileOutputStream fout = null; 
        CipherInputStream cipherIn = null; 
        Key key = null; 
        Cipher cipher = null; 

        key = getDESKeyFormFile(keyFile); 

        if (key == null) { 
            return false; 
        } 

        try { 
            cipher = Cipher.getInstance("DES"); 
              cipher.init(mode, key); 
        } catch (NoSuchPaddingException ex3) { 
            return false; 
        } catch (NoSuchAlgorithmException ex3) { 
            return false; 
        } catch (InvalidKeyException ex4) { 
            return false; 
        } 

        try { 
            fin = new FileInputStream(srcFile); 
            cipherIn = new CipherInputStream(fin,cipher); 

            fout = new FileOutputStream(destFile); 
        } catch (FileNotFoundException ex) { 
            ex.printStackTrace(); 
            return false; 
        } 


        byte[] data = new byte[1024]; 
        int len = -1; 
        int count = 0; 
        boolean isFinsh = false; // 是否完成 
        try { 
            while ((len = cipherIn.read(data)) != -1) { 
                if(len==-1){ 
                    // 完成 
                    isFinsh = true; 
                } 
                if(thread.isEnd()){ 
                    //取消,退出,处理过程 
                    break; 
                } 


                 // 暂停,在等 
                try { 
                    thread.waitForResume(); 
                } catch (InterruptedException ex5) { 
                    ex5.printStackTrace(); 
                     JOptionPane.showMessageDialog(thread.proFrame, "暂停失败!"); 
                } 


                fout.write(data, 0, len); 
                // 显示百分比,显示进度 
                count +=len; 
                thread.proFrame.jProgressBar1.setValue(count); 
                thread.proFrame.jLabel5.setText("完成字节:"+String.valueOf(thread.proFrame.jProgressBar1.getValue())); 
                thread.proFrame.jLabel6.setText("总共字节:"+String.valueOf(thread.proFrame.jProgressBar1.getMaximum())); 

//                 int pre = (int) ((proFrame.jProgressBar1.getValue()*100*100000)/(proFrame.jProgressBar1.getMaximum()*100000)); 
//                proFrame.jLabel7.setText( 
//                        String.valueOf(pre)+"%"); 


            } 

        } catch (IOException ex1) { 
            ex1.printStackTrace(); 

⌨️ 快捷键说明

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