3DES加解密-EncryptAndDecryptFile

liuwen276發表於2017-08-24
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();
}
}
}

相關文章