3DES加解密-EncryptAndDecryptFile
来源:互联网 发布:python和qt 编辑:程序博客网 时间:2024/05/10 14:50
import com.pinganfu.pvsn.common.util.PvsnLogType;
/**
*
* @author LIUWEN276
* 不能压缩文件夹,压缩文件夹用ZipCompressor
*
*/
public class EncryptAndDecryptFile {
private static Logger logger = LoggerFactory.getLogger(PvsnLogType.PVSN_COMMON.getLogName());
private static final String threeDesKey = "123456788765432112345678";
public static void main(String[] args) {
encryptFile();
decryptFile();
}
private static void encryptFile() {
String distFileName = "";
distFileName = "CCF_20140807_2000386956.zip";
String uploadZipPath = "D:/shareFile/zip/";
String uploadSrcPath = "D:/shareFile/txt/CCF_20140807_2000386956/";
List<String> srcFileName = new ArrayList<String>();
srcFileName.add("CCF_20140807_2000386956.txt");
srcFileName.add("CCF_20140807_2000386956_2.txt");
encryptFile(srcFileName, distFileName, uploadZipPath, uploadSrcPath);
}
/**
* 压缩并加密
*
*/
private static void encryptFile(List<String> srcFileName, String distFileName, String uploadZipPath,
String uploadSrcPath) {
try {
/**
* 新建 对应的目录start
*/
/*
* String[] dir = uploadZipPath.split("/"); String dirpath = ""; for
* (int i = 0; i < dir.length; i++) { dirpath += dir[i] + "/"; File
* f = new File(dirpath); if (!f.exists()) { f.mkdir(); } }
*/
File ZipPath = new File(uploadZipPath);
if (!ZipPath.exists()) {
throw new RuntimeException(uploadZipPath + "不存在!");
}
/**
* 新建 对应的目录end
*/
// 设置流以加密模式输出
OutputStream encryptOutPutStream = KTDes3Tool.encryptMode(threeDesKey, new FileOutputStream(uploadZipPath
+ distFileName));
// 设置流以压缩模式输出
org.apache.commons.io.compress.zip.ZipOutputStream zos = new org.apache.commons.io.compress.zip.ZipOutputStream(
encryptOutPutStream);
zos.setEncoding("UTF-8");
DataInputStream dis = null;
for (String fileName : srcFileName) {
File f = new File(uploadSrcPath + fileName);
dis = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
zos.putNextEntry(new org.apache.commons.io.compress.zip.ZipEntry(f.getName()));
int c;
while ((c = dis.read()) != -1) {
zos.write(c);
}
}
try {
zos.closeEntry();
zos.close();
dis.close();
} catch (IOException e1) {
logger.error("error occurred in PvsnFileUtils#batchZipCompressApache while closing BufferedOutputStream.");
}
} catch (Exception e) {
logger.error("压缩并加密 异常,e={}", e.getMessage());
e.printStackTrace();
}
}
private static void decryptFile() {
String srcFileName = "";
String distFileName = "";
String downloadZipPath = "D:/shareFile/zip/";
String downloadTxtPath = "D:/shareFile/zip/";
srcFileName = "aaa.zip";
distFileName = "aaa.zip";
decryptFile(srcFileName, distFileName, downloadZipPath, downloadTxtPath);
}
/**
* 解密并解压缩
*
*/
private static void decryptFile(String srcFileName, String distFileName, String downloadZipPath,
String downloadTxtPath) {
InputStream decryptInputStream;
try {
decryptInputStream = KTDes3Tool
.decryptMode(threeDesKey, new FileInputStream(downloadZipPath + srcFileName));
// 设置流以解密模式输出
ZipInputStream zipIn = new ZipInputStream(decryptInputStream);
BufferedInputStream Bin = new BufferedInputStream(zipIn);
// 解压缩到文件
File Fout = null;
ZipEntry entry = null;
while ((entry = zipIn.getNextEntry()) != null && !entry.isDirectory()) {
Fout = new File(downloadTxtPath, entry.getName());
if (!Fout.exists()) {
(new File(Fout.getParent())).mkdirs();
}
FileOutputStream out = new FileOutputStream(Fout);
BufferedOutputStream Bout = new BufferedOutputStream(out);
int b;
while ((b = Bin.read()) != -1) {
Bout.write(b);
}
Bout.close();
out.close();
System.out.println(Fout + "解压成功");
}
Bin.close();
zipIn.close();
decryptInputStream.close();
} catch (Exception e) {
logger.error("解密并解压缩 异常,e={}", e.getMessage());
e.printStackTrace();
}
}
}
阅读全文
0 0
- 3DES加解密-EncryptAndDecryptFile
- 关于DES加解密,3DES加解密
- DES、3DES 加解密;MAC算法
- PHP des 3des加解密
- java 3DES 加解密
- openssl 3des 加解密
- iOS 3DES加解密
- 3DES加解密案例
- 3DES加解密算法
- 3DES加解密-KTDes3Tool
- 3DES加解密-ZipCompressor
- 3Des加解密,压缩文件
- 3DES 加解密 Java版
- 3DES 加解密 PHP版
- java 与 c# 3des 加解密
- java实现3des加解密
- RSA加密和3DES加解密
- ios java 3DES 加解密
- 前端限制input只能输入数字
- php foreach循环关于&引用的问题
- ContentProvider组件-ANdroid
- 微信小程序周报(第十二期)-极乐小程序商店出品
- No MyBatis mapper was found in '' package. Please check your configura, Could not autowire field
- 3DES加解密-EncryptAndDecryptFile
- 关于C语言中的E-R图-------详解
- java练习——符号生成图形1
- Flume1.5.0入门:安装、部署、及flume的案例
- 431只能做参考电压,不能把它当供电电源用了。
- 随便整理
- 完全背包问题-动态规划
- 虚拟币开发专题(如何在windows平台下编译比特币bitcoin客户端)
- 产品经理究竟是干什么的