【Java核心技术】CipherInputStream与CipherOutputStream 加密流2

http://qinshanwu.iteye.com/blog/373556

http://qinshanwu.iteye.com/blog/373557

在加解密操作时,密文总是8的倍数,比如明文有51个字节,加密后就会有56个字节,des会自动加上其它字符(空格)来补上,所以加密解密前内容可能有些不一样,需要注意. 


在这主要是用到两个流: 
CipherInputStream cin=new CipherInputStream(in,c); 
CipherOutputStream cout=new CipherOutputStream(out,c); 

CipherOutputStream 由一个 OutputStream 和一个 Cipher 组成 ,write() 方法在将数据写出到基础 OutputStream 之前先对该数据进行处理(加密或解密) , 
同样CipherInputStream是由InputStream和一个Cipher组成,read()方法在读入时,对数据进行加解密操作. 

package cn.ls.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class DES4 {
	private static String Algorithm = "DESede"; // 定义 加密算法,可用DES,DESede,Blowfish

	static {
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
	}

	// 生成密钥, 注意此步骤时间比较长
	public static byte[] getKey() throws Exception {
		KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
		SecretKey deskey = keygen.generateKey();
		return deskey.getEncoded();
	}

	/**
	 * 加密
	 * 
	 * @param enfile
	 *            要加密的文件
	 * @param defile
	 *            加密后的文件
	 * @param key
	 *            密钥
	 * @throws Exception
	 */
	public static void encode(String enfile, String defile, byte[] key)
			throws Exception {
		// 秘密(对称)密钥(SecretKey继承(key))
		// 根据给定的字节数组构造一个密钥。
		SecretKey deskey = new SecretKeySpec(key, Algorithm);
		// 生成一个实现指定转换的 Cipher 对象。Cipher对象实际完成加解密操作
		Cipher c = Cipher.getInstance(Algorithm);
		// 用密钥初始化此 cipher
		c.init(Cipher.ENCRYPT_MODE, deskey);

		byte[] buffer = new byte[1024];
		FileInputStream in = new FileInputStream(enfile);
		OutputStream out = new FileOutputStream(defile);
		 
		CipherInputStream cin = new CipherInputStream(in, c);
		int i;
		while ((i = cin.read(buffer)) != -1) {
			out.write(buffer, 0, i);
		}
		out.close();
		cin.close();
	}

	// 解密
	public static void decode(String file, String defile, byte[] key)
			throws Exception {

		// DES算法要求有一个可信任的随机数源
		SecureRandom sr = new SecureRandom();
		// 创建一个 DESKeySpec 对象,指定一个 DES 密钥
		DESKeySpec ks = new DESKeySpec(key);
		// 生成指定秘密密钥算法的 SecretKeyFactory 对象。
		SecretKeyFactory factroy = SecretKeyFactory.getInstance(Algorithm);
		// 根据提供的密钥规范(密钥材料)生成 SecretKey 对象,利用密钥工厂把DESKeySpec转换成一个SecretKey对象
		SecretKey sk = factroy.generateSecret(ks);
		// 生成一个实现指定转换的 Cipher 对象。Cipher对象实际完成加解密操作
		Cipher c = Cipher.getInstance(Algorithm);
		// 用密钥和随机源初始化此 cipher
		c.init(Cipher.DECRYPT_MODE, sk, sr);

		byte[] buffer = new byte[1024];
		FileInputStream in = new FileInputStream(file);
		OutputStream out = new FileOutputStream(defile);
		CipherOutputStream cout = new CipherOutputStream(out, c);
		int i;
		while ((i = in.read(buffer)) != -1) {
			cout.write(buffer, 0, i);
		}
		cout.close();
		in.close();
	}

	public static void main(String[] args) throws Exception {
		byte[] key = "09823541".getBytes(); //字节数必须是8的整数倍 
		// 文件加密
		encode("F:/a.html", "F:/b.html", key);

		// 文件解密
		decode("F:/b.html", "F:/c.html", key);
	}
}


  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值