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;
}
}