package de.idyl.crypto.zip;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import de.idyl.crypto.zip.impl.AESEncrypter;
import de.idyl.crypto.zip.impl.AESEncrypterBC;
import de.idyl.crypto.zip.impl.ExtZipEntry;
import de.idyl.crypto.zip.impl.ExtZipOutputStream;
import de.idyl.crypto.zip.impl.ZipFileEntryInputStream;
/**
* Create ZIP archive containing AES-256 encrypted entries. <br>
* One instance of this class represents one encrypted ZIP file, that can receive add() method calls
* that must be followed by one final call to close() to write the final archive part and close the
* output stream.
*
* TODO - support 128 + 192 keys
*
* @author olaf@merkert.de
* @author jean-luc.cooke@greenparty.ca
*/
public class AesZipFileEncrypter {
private static final Logger LOG = Logger.getLogger(AesZipFileEncrypter.class.getName());
// --------------------------------------------------------------------------
protected ExtZipOutputStream zipOS;
/**
*
* @param pathName
* to output zip file (aes encrypted zip file)
*/
public AesZipFileEncrypter(String pathName) throws IOException {
zipOS = new ExtZipOutputStream(new File(pathName));
}
/**
*
* @param outFile
* output file (aes encrypted zip file)
*/
public AesZipFileEncrypter(File outFile) throws IOException {
zipOS = new ExtZipOutputStream(outFile);
}
// --------------------------------------------------------------------------
protected void add(ExtZipEntry zipEntry, InputStream zipData) throws IOException,
UnsupportedEncodingException {
zipOS.putNextEntry(zipEntry);
byte[] data = new byte[1024];
int read = zipData.read(data);
while (read != -1) {
zipOS.writeBytes(data, 0, read);
read = zipData.read(data);
}
}
protected void add(ZipFile inFile, String password) throws IOException,
UnsupportedEncodingException {
ZipFileEntryInputStream zfe = new ZipFileEntryInputStream(inFile);
Enumeration<? extends ZipEntry> en = inFile.entries();
while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();
zfe.nextEntry(ze);
add(ze, zfe, password);
}
zfe.close();
}
// TODO - zipEntry might use extended local header
protected void add(ZipEntry zipEntry, ZipFileEntryInputStream zipData, String password)
throws IOException, UnsupportedEncodingException {
AESEncrypter aesEncrypter = new AESEncrypterBC(password.getBytes("iso-8859-1"));
ExtZipEntry entry = new ExtZipEntry(zipEntry.getName());
entry.setMethod(zipEntry.getMethod());
entry.setSize(zipEntry.getSize());
entry.setCompressedSize(zipEntry.getCompressedSize() + 28);
entry.setTime(zipEntry.getTime());
entry.initEncryptedEntry();
zipOS.putNextEntry(entry);
// ZIP-file data contains: 1. salt 2. pwVerification 3. encryptedContent 4. authenticationCode
zipOS.writeBytes(aesEncrypter.getSalt());
zipOS.writeBytes(aesEncrypter.getPwVerification());
byte[] data = new byte[1024];
int read = zipData.read(data);
while (read != -1) {
aesEncrypter.encrypt(data, read);
zipOS.writeBytes(data, 0, read);
read = zipData.read(data);
}
byte[] finalAuthentication = aesEncrypter.getFinalAuthentication();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("finalAuthentication=" + Arrays.toString(finalAuthentication) + " at pos="
+ zipOS.getWritten());
}
zipOS.writeBytes(finalAuthentication);
}
/**
* Add un-encrypted + un-zipped file to encrypted zip file.<br>
*
* @param file to add
* @param pathForEntry to be used for addition of the file (path within zip file)
* @param password to be used for encryption
*/
public void add(File file, String pathForEntry, String password) throws IOException, UnsupportedEncodingException {
FileInputStream fis = new FileInputStream(file);
try {
add(pathForEntry, fis, password);
} finally {
fis.close();
}
}
/**
* Add un-encrypted + un-zipped file to encrypted zip file.
*
* @param file to add, provides the path of the file within the zip file via its getPath()
* @param password to be used for encryption
*/
public void add(File file, String password) throws IOException, UnsupportedEncodingException {
FileInputStream fis = new FileInputStream(file);
try {
add(file.getPath(), fis, password);
} finally {
fis.close();
}
}
/**
* Add un-encrypted + un-zipped InputStream contents as file "name" to encrypted zip file.
*
* @param name of the new zipEntry within the zip file
* @param is provides the data to be added
* @param password to be used for encryption
*/
public void add(String name, InputStream is, String password) throws IOException, UnsupportedEncodingException {
AESEncrypter aesEncrypter = new AESEncrypterBC(password.getBytes("iso-8859-1"));
// Compress contents of inputStream and report on bytes read
// we need to first compress to know details of entry
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(bos, new Deflater(9, true), 8 * 1024);
int read, inputLen = 0;
byte[] buf = new byte[8 * 1024];
while ((read = is.read(buf)) > 0) {
inputLen += read;
dos.write(buf, 0, read);
}
dos.close();
byte[] data = bos.toByteArray();
ExtZipEntry entry = new ExtZipEntry(name);
entry.setMethod(ZipEntry.DEFLATED);
entry.setSize(inputLen);
entry.setCompressedSize(data.length + 28);
entry.setTime((new java.util.Date()).getTime());
entry.initEncryptedEntry();
zipOS.putNextEntry(entry);
// ZIP-file data contains: 1. salt 2. pwVerification 3. encryptedContent 4. authenticationCode
zipOS.writeBytes(aesEncrypter.getSalt());
zipOS.writeBytes(aesEncrypter.getPwVerification());
aesEncrypter.encrypt(data, data.length);
zipOS.writeBytes(data, 0, data.length);
byte[] finalAuthentication = aesEncrypter.getFinalAuthentication();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("finalAuthentication=" + Arrays.toString(finalAuthentication) + " at pos="
+ zipOS.getWritten());
}
zipOS.writeBytes(finalAuthentication);
}
// --------------------------------------------------------------------------
/**
* add one file to encrypted zip file
*
* @param pathToFile
* the file to add
* @param password
* the password used for encryption
* @throws IOException
public void add(File pathToFile, String password) throws IOException {
File outZipFile = new File(pathToFile + ".zip");
zip(pathToFile, outZipFile);
addAll(outZipFile, password);
outZipFile.delete();
}
*/
// --------------------------------------------------------------------------
/**
* Zip contents of inFile to outFile.
*/
public static void zip(File inFile, File outFile) throws IOException {
FileInputStream fin = new FileInputStream(inFile);
FileOutputStream fout = new FileOutputStream(outFile);
ZipOutputStream zout = new ZipOutputStream(fout);
zout.putNextEntry(new ZipEntry(inFile.getName()));
byte[] buffer = new byte[1024];
int len;
while ((len = fin.read(buffer)) > 0) {
zout.write(buffer, 0, len);
}
zout.closeEntry();
zout.close();
fin.close();
}
// --------------------------------------------------------------------------
/**
* Take all elements (files) from zip file and add them ENCRYPTED
没有合适的资源?快使用搜索试试~ 我知道了~
winzipaes.rar
共15个文件
java:14个
jar:1个
5星 · 超过95%的资源 需积分: 9 45 下载量 13 浏览量
2011-07-21
01:17:43
上传
评论
收藏 25KB RAR 举报
温馨提示
与其它jar文件配合使用,解决java ZIP加解密问题
资源推荐
资源详情
资源评论
收起资源包目录
winzipaes.rar (15个子文件)
winzipaes
lcrypto-jdk15.jar 31KB
winzipaes_src_20100728
src
de
idyl
crypto
zip
impl
ByteArrayHelper.java 2KB
CentralDirectoryEntry.java 8KB
AESEncrypter.java 291B
AESDecrypter.java 223B
AESCryptoBase.java 660B
AESEncrypterBC.java 5KB
ExtZipEntry.java 4KB
ExtZipOutputStream.java 5KB
ZipFileEntryInputStream.java 3KB
ExtRandomAccessFile.java 2KB
ZipConstants.java 509B
AESDecrypterBC.java 4KB
AesZipFileEncrypter.java 9KB
AesZipFileDecrypter.java 8KB
共 15 条
- 1
资源评论
- lcq1052014-09-05里边的jar文件没法使用,反编译不了,源码也缺包!还我积分!!
- krio_love2015-08-12很好 感觉还不错
- hemengru2013-01-09很好,解决大问题
- 跳梁小丑-1232013-01-15很好,解决大问题
- 风中有2015-03-19解决了我的问题
超级木单车
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功