?? desjiajiemi.txt
字號:
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 + -