Java DES 加解密字符串、文件 工具类通用

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * DES加解密工具类 单例工具类
 * 
 * 1、通过指定密匙加密字符串、解密字符串 
 * 2、通过指定密匙加密文件、解密文件
 * 
 * @author kaifang
 * 
 */
public class DESUtil {

	/**
	 * 加密解密所需密匙
	 */
	private Key key;
	/**
	 * des加解密工具类实例
	 */
	private static DESUtil desUtil;

	/**
	 * 获取des工具类实例的方法
	 * 
	 * @param key
	 *            加解密密匙
	 * @return des工具类实例
	 */
	public synchronized static DESUtil getInstance(String key) {
		if (desUtil == null) {
			desUtil = new DESUtil(key);
		}
		return desUtil;
	}

	/**
	 * des工具类私有构造
	 * 
	 * @param str
	 */
	private DESUtil(String str) {
		setKey(str); // 生成密匙
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	/**
	 * 根据参数生成 KEY
	 */
	private void setKey(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(e);
		}
	}

	/**
	 * 加密 String 明文输入 ,String 密文输出
	 */
	private String encryptStr(String strMing) {
		byte[] byteMi = null;
		byte[] byteMing = null;
		String strMi = "";
		BASE64Encoder base64en = new BASE64Encoder();
		try {
			byteMing = strMing.getBytes("UTF8");
			byteMi = this.encryptByte(byteMing);
			strMi = base64en.encode(byteMi);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			base64en = null;
			byteMing = null;
			byteMi = null;
		}
		return strMi;
	}

	/**
	 * 解密 以 String 密文输入 ,String 明文输出
	 * 
	 * @param strMi
	 * @return
	 */
	private String decryptStr(String strMi) {
		BASE64Decoder base64De = new BASE64Decoder();
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = "";
		try {
			byteMi = base64De.decodeBuffer(strMi);
			byteMing = this.decryptByte(byteMi);
			strMing = new String(byteMing, "UTF8");
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			base64De = null;
			byteMing = null;
			byteMi = null;
		}
		return strMing;
	}

	/**
	 * 加密以 byte[] 明文输入 ,byte[] 密文输出
	 * 
	 * @param byteS
	 * @return
	 */
	private byte[] encryptByte(byte[] byteS) {
		byte[] byteFina = null;
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byteFina = cipher.doFinal(byteS);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	/**
	 * 解密以 byte[] 密文输入 , 以 byte[] 明文输出
	 * 
	 * @param byteD
	 * @return
	 */
	private byte[] decryptByte(byte[] byteD) {
		Cipher cipher;
		byte[] byteFina = null;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byteFina = cipher.doFinal(byteD);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	/**
	 * 文件 file 进行加密并保存目标文件 destFile 中
	 * 
	 * @param file
	 *            要加密的文件 如 c:/test/srcFile.txt
	 * @param destFile
	 *            加密后存放的文件名 如 c:/ 加密后文件 .txt
	 */
	private void encryptFile(File file, File destFile) {
		InputStream is = null;
		OutputStream out = null;
		CipherInputStream cis = null;
		try {
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, this.key);
			is = new FileInputStream(file);
			out = new FileOutputStream(destFile);
			cis = new CipherInputStream(is, cipher);
			byte[] buffer = new byte[1024];
			int r;
			while ((r = cis.read(buffer)) > 0) {
				out.write(buffer, 0, r);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				cis.close();
				is.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 文件采用 DES 算法解密文件
	 * 
	 * @param file
	 *            已加密的文件 如 c:/ 加密后文件 .txt *
	 * @param destFile
	 *            解密后存放的文件名 如 c:/ test/ 解密后文件 .txt
	 */
	private void decryptFile(File file, File dest) {
		InputStream is = null;
		OutputStream out = null;
		CipherOutputStream cos = null;
		try {
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, this.key);
			is = new FileInputStream(file);
			out = new FileOutputStream(dest);
			cos = new CipherOutputStream(out, cipher);
			byte[] buffer = new byte[1024];
			int r;
			while ((r = is.read(buffer)) >= 0) {
				cos.write(buffer, 0, r);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				cos.close();
				out.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * des解密字符串的方法
	 * 
	 * @param key
	 *            解密需要的密匙
	 * @param encrypt
	 *            密文
	 * @return 明文
	 */
	public static String getDecodeStr(String key, String encrypt) {
		return DESUtil.getInstance(key).decryptStr(encrypt);
	}

	/**
	 * des加密字符串的方法
	 * 
	 * @param key
	 *            加密所需密匙
	 * @param decrypt
	 *            明文
	 * @return 明文
	 */
	public static String getEncodeStr(String key, String decrypt) {
		return DESUtil.getInstance(key).encryptStr(decrypt);
	}

	/**
	 * des解密文件
	 * 
	 * @param key
	 *            解密所需密匙
	 * @param miFile
	 *            密文文件
	 * @param mingFile
	 *            明文文件
	 * @throws Exception
	 */
	public static void getDecodeFile(String key, File miFile, File mingFile) {
		DESUtil.getInstance(key).decryptFile(miFile, mingFile);
	}

	/**
	 * des加密文件
	 * 
	 * @param key
	 *            加密所需密匙
	 * @param mingFile
	 *            明文文件
	 * @param miFile
	 *            密文文件
	 * @throws Exception
	 */
	public static void getEncodeFile(String key, File mingFile, File miFile) {
		DESUtil.getInstance(key).encryptFile(mingFile, miFile);
	}

	public static void main(String[] args) throws Exception {
		/*
		 * DESUtil des = new DESUtil("kk"); // DES 加密文件 //
		 * des.encryptFile("G:/test.doc", "G:/ 加密 test.doc"); // DES 解密文件 //
		 * des.decryptFile("G:/ 加密 test.doc", "G:/ 解密 test.doc"); //String str1
		 * = "爱凯凯"; // DES 加密字符串 //String str2 = des.encryptStr(str1); // DES
		 * 解密字符串 String deStr = des.decryptStr("mm6DlWB9DJKxXeS5Xlh4TQ==");
		 * 
		 * //System.out.println(" 加密前: " + str1); //System.out.println(" 加密后: "
		 * + str2); System.out.println(" 解密后: " + deStr);
		 */

		// System.out.println(DESUtil.getEncodeStr("kk", "爱凯凯"));
		System.out.println(DESUtil.getDecodeStr("kk", "mm6DlWB9DJKxXeS5Xlh4TQ=="));
	}
}


无需导入任何jar包哈,都是jdk自带的加密类。若是报错,请看:

http://blog.csdn.net/jbxiaozi/article/details/7351768


本文参考自:http://blog.csdn.net/shibenjie/article/details/5365355

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值