java DES加密解密文件

在android開發中或者java開發中,經常用一些加密或者解密文件的,下面是我最近測試的一個DES加密解密文件的方式,記錄一下,防止以後忘記了!

下面直接貼代碼把,詳細的註釋都寫有:

package com.spring.des;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/***
 * Des文件加密解密
 * 
 * @author spring sky<br>
 * Email:[email protected]<br>
 * QQ:840950105
 * 
 */
public class DesFileEncrypt {
	/**
	 * 要加密的文件路徑列表
	 */
	public static String[] filePath = { "D:/sasdfp.sql" };
	/**
	 * 加密後的文件路徑列表
	 */
	public static String[] outFilePath = new String[filePath.length];
	private static final String KEY = "spring sky";

	public DesFileEncrypt() {
		super();
		getKey(KEY);
		initCipher();
		//初始化開始加密文件
		crateEncryptFile();
	}

	private Key key;
	
	/***
	 * 解密密碼
	 */
	private Cipher cipherDecrypt;
	/**
	 * 加密密碼
	 */
	private Cipher cipherEncrypt;

	/**
	 * 加密文件平且記錄加密後的文件路徑
	 * */
	private void crateEncryptFile() {
		String outPath = null;
		for (int i = 0; i < filePath.length; i++) {
			try {
				outPath = filePath[i].substring(0,filePath[i].lastIndexOf("."))+".bin";
				encrypt(filePath[i], outPath);
				outFilePath[i] = outPath;
				System.out.println(filePath[i]+"加密完成,加密後的文件是:"+outFilePath[i]);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("=========================加密完成=======================");
		
	}

	/**
	 * 加密文件的核心
	 * 
	 * @param file
	 *            要加密的文件
	 * @param destFile
	 *            加密後存放的文件名
	 */
	public void encrypt(String file, String destFile) throws Exception {
		InputStream is = new FileInputStream(file);
		OutputStream out = new FileOutputStream(destFile);

		CipherInputStream cis = new CipherInputStream(is, cipherEncrypt);
		byte[] buffer = new byte[1024];
		int r;
		while ((r = cis.read(buffer)) > 0) {
			out.write(buffer, 0, r);
		}
		cis.close();
		is.close();
		out.close();
	}

	/***
	 * 解密文件
	 * @param destFile
	 */
	public void decrypt(String destFile) {
		try {
			InputStream is = new FileInputStream(destFile);
			CipherInputStream cis = new CipherInputStream(is, cipherDecrypt);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					cis));
			String line = null;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
			reader.close();
			cis.close();
			is.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void initCipher() {
		try {
			// 加密的cipher
			cipherEncrypt = Cipher.getInstance("DES");
			cipherEncrypt.init(Cipher.ENCRYPT_MODE, this.key);
			// 解密的cipher
			cipherDecrypt = Cipher.getInstance("DES");
			cipherDecrypt.init(Cipher.DECRYPT_MODE, this.key);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 自定義一個key
	 * 
	 * @param string
	 */
	public Key getKey(String keyRule) {
		// Key key = null;
		byte[] keyByte = keyRule.getBytes();
		// 創建一個空的八位數組,默認情況下爲0
		byte[] byteTemp = new byte[8];
		// 將用戶指定的規則轉換成八位數組
		for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
			byteTemp[i] = keyByte[i];
		}
		key = new SecretKeySpec(byteTemp, "DES");
		return key;
	}

	/**
	 * 解密密碼
	 * 
	 * @return
	 */
	public Cipher getCipherEdcrypt() {
		return cipherDecrypt;
	}

	/**
	 * 加密密碼
	 * 
	 * @return
	 */
	public Cipher getCipherEncrypt() {
		return cipherEncrypt;
	}

	/***
	 * 測試加密解密
	 * @param args
	 */
	public static void main(String[] args) {
		DesFileEncrypt desFileEncrypt = new DesFileEncrypt();
		desFileEncrypt.decrypt(outFilePath[0]);  //解密第一個文件平且測試解密後的結果
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章