您的位置:首页 > 编程语言 > Java开发

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

2012-06-03 19:11 309 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息