3DES加解密-EncryptAndDecryptFile
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();
}
}
}
相關文章
- PHP和.NET通用的加密解密函式類,均使用3DES加解密PHP加密解密函式3D
- Golang 裡的 AES、DES、3DES 加解密,支援 ECB、CBC 等多種模式組合Golang3D解密模式
- 【Java加解密系列】- SM2加解密Java解密
- Java 3DES 加解/密程式Java3D
- 前端加解密前端解密
- JAVA和C# 3DES加密解密JavaC#3D加密解密
- 客戶端加解密客戶端解密
- 遊戲異或加解密遊戲解密
- 加解密演算法解密演算法
- Java RSA 分段加解密Java解密
- 移動端加解密解密
- Golang實現ECB模式3DES演算法Golang模式3D演算法
- 加解密演算法分析解密演算法
- 對稱EDS加解密方法解密
- NodeJS加解密之CryptoNodeJS解密
- Java之DES加解密解析Java解密
- Java之RSA加解密解析Java解密
- C# RSA 分段加解密C#解密
- Openssl RSA基本加解密操作解密
- vue 核心加解密工具類 方法Vue解密
- AES加解密使用總結解密
- PHP 支援加解密的函式PHP解密函式
- C語言(檔案加解密)C語言解密
- .NET AES加解密(128位)解密
- jive對密碼的加解密?密碼解密
- C#.NET 字串加解密程式C#字串解密
- python各種加解密方法Python解密
- 安全閘道器 透明加解密解密
- TEA、XTEA、XXTEA加解密過程解密
- 小規模DES手寫加解密解密
- VUE專案 AES加解密(小白版)Vue解密
- OpenSSL 使用AES對檔案加解密解密
- 前後端引數加解密方案後端解密
- CTF中常見的加解密(經典)解密
- Python AES - base64 加解密Python解密
- 一個聚合的加解密工具類解密
- Flex中的Base64加解密Flex解密
- 記錄 Openssl 替代 Mcrypt 3DES CBC 演算法解決方案3D演算法