文件加密解密三

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

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

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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 = "DES"; // 定义 加密算法,可用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);
//从 InputStream 和 Cipher 构造 CipherInputStream。
// read() 方法在从基础 InputStream 读入已经由 Cipher 另外处理(加密或解密)
CipherInputStream cin=new CipherInputStream(in,c);
int i;
while((i=cin.read(buffer))!=-1){
// for(int k=0;k<i;k++){
// System.out.print(buffer[k]+" ");
// }
out.write(buffer,0,i);
}
out.close();
cin.close();
}

// 解密
public static void decode(String file,String defile, byte[] key) throws Exception {
// //根据给定的字节数组构造一个密钥。
// SecretKey deskey=new SecretKeySpec(key,Algorithm);
// //生成一个实现指定转换的 Cipher 对象。
// Cipher c=Cipher.getInstance(Algorithm);
// //用密钥初始化此 cipher
// c.init(Cipher.DECRYPT_MODE, deskey);


//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 由一个 OutputStream 和一个 Cipher 组成
//write() 方法在将数据写出到基础 OutputStream 之前先对该数据进行处理(加密或解密)
CipherOutputStream cout=new CipherOutputStream(out,c);
int i;
while((i=in.read(buffer))!=-1){
// for(int k=0;k<i;k++){
// System.out.print(buffer[k]+" ");
// }
cout.write(buffer,0,i);
}
cout.close();
in.close();
}
public static void main(String[] args)throws Exception {
byte[] key="00000000".getBytes();
//文件加密
encode("E:/test/des.txt","E:/test/encrypt.txt",key);

//文件解密
decode("E:/test/encrypt.txt","E:/test/decrypt.txt",key);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值