Java Des算法实现高速文件加密解密

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;

//java给文件加密使用的md5的加密算法,实例如下:
public class FileEncryptionUtil {
	Key key;

	public FileEncryptionUtil(String str) {
		getKey(str);// 生成密匙
		System.out.println(key);
	}

	/**
	 * 根据参数生成KEY
	 */
	public void getKey(String strKey) {
		try {
			KeyGenerator _generator = KeyGenerator.getInstance("DES");
			_generator.init(new SecureRandom(strKey.getBytes()));
			this.key = _generator.generateKey();
			_generator = null;
		} catch (Exception e) {
			throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
		}
	}

	/**
	 * 文件file进行加密并保存目标文件destFile中
	 * 
	 * @param file
	 *            要加密的文件 如c:/test/srcFile.txt
	 * @param destFile
	 *            加密后存放的文件名 如c:/加密后文件.txt
	 */
	public void encrypt(String file, String destFile) throws Exception {
		long times = System.currentTimeMillis();
		// Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
		// 参数String transformation的格式是“算法/工作模式/填充模式”,不同的算法支持不同的工作模式以及填充模式。
		Cipher cipher = Cipher.getInstance("DES");// 实例化Cipher对象
		cipher.init(Cipher.ENCRYPT_MODE, this.key);// 用于将Cipher初始化为加密模式的常量
													// 初始化Cipher对象,用于包装
		InputStream is = new FileInputStream(file);
		int size = is.available() / 1024 / 1024;
		System.out.println("文件大小:" + size + "MB");
		OutputStream out = new FileOutputStream(destFile);
		CipherInputStream cis = new CipherInputStream(is, cipher);
		byte[] buffer = new byte[1024];
		int r;
		while ((r = cis.read(buffer)) > 0) {
			out.write(buffer, 0, r);
		}

		long allTimes = (System.currentTimeMillis() - times) / 1000;
		long speed = size / allTimes;
		System.out.println("完成加密!");
		System.out.println("总共耗时:" + allTimes + "秒");
		System.out.println("转换速率:" + speed + "MB/秒");

		cis.close();
		is.close();
		out.close();
	}

	/**
	 * 文件采用DES算法解密文件
	 * 
	 * @param file
	 *            已加密的文件 如c:/加密后文件.txt * @param destFile 解密后存放的文件名 如c:/
	 *            test/解密后文件.txt
	 */
	public void decrypt(String file, String dest) throws Exception {
		long times = System.currentTimeMillis();
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.DECRYPT_MODE, this.key);// 用于将Cipher初始化为解密模式的常量
		InputStream is = new FileInputStream(file);
		int size = is.available() / 1024 / 1024;
		System.out.println("文件大小:" + size + "MB");
		OutputStream out = new FileOutputStream(dest);
		CipherOutputStream cos = new CipherOutputStream(out, cipher);
		byte[] buffer = new byte[1024];
		int r;
		while ((r = is.read(buffer)) >= 0) {
			cos.write(buffer, 0, r);
		}

		long allTimes = (System.currentTimeMillis() - times) / 1000;
		long speed = size / allTimes;
		System.out.println("完成解密!");
		System.out.println("总共耗时:" + allTimes + "秒");
		System.out.println("转换速率:" + speed + "MB/秒");

		cos.close();
		out.close();
		is.close();
	}

	public static void main(String[] args) throws Exception {
		FileEncryptionUtil td = new FileEncryptionUtil("****************");
		// td.encrypt("f:/1.avi", "f:/1解密.avi"); // 加密
		td.decrypt("f:/1解密.avi", "f:/11.avi"); // 解密

	}
}

 

你可能感兴趣的:(JavaSe)