文件加解密

package com.asc;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* 加解密Excel
* @author liuli
*
*/
public class ExcelReaderTest {
public static void main(String args[]){
// encryptExcelFile();
decryptExcelFile();
}
/**
* excel加密
*/
private static void encryptExcelFile() {
FileInputStream inputStream=null;//声明一个输入流
FileOutputStream outputStream=null;//声明一个输出流
//构造密钥,指定算法为AES
SecretKeySpec skeySpec=new SecretKeySpec("AgilesClientLons".getBytes(), "AES");
try {
//设置密码算法为:AES,模式为ECB,填充模式为PKCS5Padding
Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
//初始化cipher为加密算法
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
inputStream=new FileInputStream(new File("f://excelTest.xlsx"));//得到未加密码的文件流
outputStream=new FileOutputStream(new File("f://excel_encrypt.xlsx"));
//构造一个缓冲区
byte [] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
outputStream.write(cipher.update(buffer,0,len));
}
outputStream.write(cipher.doFinal());
outputStream.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(null!=inputStream){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(null!=outputStream){
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

/**
* excel解密
*/
private static void decryptExcelFile() {
FileInputStream inputStream = null;// 声明一个输入流
FileOutputStream outputStream = null;// 声明一个输出流
// 构造密钥,指定算法为AES
SecretKeySpec skeySpec = new SecretKeySpec(
"AgilesClientLons".getBytes(), "AES");
try {
// 设置密码算法为AES,模式为ECB,填充模式为:PKCS5Padding
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 初始化设置为解密
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
inputStream = new FileInputStream(new File("F://014901//20110302124720136_TSKF_SAL_CUR_20101201.XLS"));// 得到test.xls的文件流
outputStream = new FileOutputStream("F://014901_decrypt//20110302124720136_TSKF_SAL_CUR_20101201_decrypt.xls");
// 声明一个缓冲区
byte[] buffer = new byte[1024];
int lenght = 0;
while ((lenght = inputStream.read(buffer)) != -1) {
outputStream.write(cipher.update(buffer, 0, lenght));//复制
}
outputStream.write(cipher.doFinal());// 结束加密
outputStream.flush();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
/**
* file change byte
*
* @param f
* @return
*/
public byte[] getBytesFromFile(File f) {
if (f == null) {
return null;
}

try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(2046);

byte[] b = new byte[2046];
int n;
while ((n = stream.read(b)) != -1) {
out.write(b, 0, n);
}
stream.close();
out.close();

return out.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

/**
* byte change file
*
* @param b
* @param outputFile
* @return
*/

public boolean getFileFromBytes(byte[] b, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return false;
}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值