Java Code Examples for net.lingala.zip4j.util.Zip4jConstants#ENC_METHOD_AES

The following examples show how to use net.lingala.zip4j.util.Zip4jConstants#ENC_METHOD_AES . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnzipEngine.java    From AndroidZip with Apache License 2.0 7 votes vote down vote up
public void checkCRC() throws ZipException {
	if (fileHeader != null) {
		if (fileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) {
			if (decrypter != null && decrypter instanceof AESDecrypter) {
				byte[] tmpMacBytes = ((AESDecrypter)decrypter).getCalculatedAuthenticationBytes();
				byte[] storedMac = ((AESDecrypter)decrypter).getStoredMac();
				byte[] calculatedMac = new byte[InternalZipConstants.AES_AUTH_LENGTH]; 
				
				if (calculatedMac == null || storedMac == null) {
					throw new ZipException("CRC (MAC) check failed for " + fileHeader.getFileName());
				}
				
				System.arraycopy(tmpMacBytes, 0, calculatedMac, 0, InternalZipConstants.AES_AUTH_LENGTH);
				
				if (!Arrays.equals(calculatedMac, storedMac)) {
					throw new ZipException("invalid CRC (MAC) for file: " + fileHeader.getFileName());
				}
			}
		} else {
			long calculatedCRC = crc.getValue() & 0xffffffffL;
			if (calculatedCRC != fileHeader.getCrc32()) {
				String errMsg = "invalid CRC for file: " + fileHeader.getFileName();
				if (localFileHeader.isEncrypted() && 
						localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) {
					errMsg += " - Wrong Password?";
				}
				throw new ZipException(errMsg);
			}
		}
	}
}
 
Example 2
Source File: CipherOutputStream.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
private void initEncrypter() throws ZipException {
	if (!zipParameters.isEncryptFiles()) {
		encrypter = null;
		return;
	}
	
	switch (zipParameters.getEncryptionMethod()) {
	case Zip4jConstants.ENC_METHOD_STANDARD:
		// Since we do not know the crc here, we use the modification time for encrypting.
		encrypter = new StandardEncrypter(zipParameters.getPassword(), (localFileHeader.getLastModFileTime() & 0x0000ffff) << 16);
		break;
	case Zip4jConstants.ENC_METHOD_AES:
		encrypter = new AESEncrpyter(zipParameters.getPassword(), zipParameters.getAesKeyStrength());
		break;
	default:
		throw new ZipException("invalid encprytion method");
	}
}
 
Example 3
Source File: ZipEngine.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
private void checkParameters(ZipParameters parameters) throws ZipException {
	
	if (parameters == null) {
		throw new ZipException("cannot validate zip parameters");
	}
	
	if ((parameters.getCompressionMethod() != Zip4jConstants.COMP_STORE) && 
			parameters.getCompressionMethod() != Zip4jConstants.COMP_DEFLATE) {
		throw new ZipException("unsupported compression type");
	}
	
	if (parameters.getCompressionMethod() == Zip4jConstants.COMP_DEFLATE) {
		if (parameters.getCompressionLevel() < 0 && parameters.getCompressionLevel() > 9) {
			throw new ZipException("invalid compression level. compression level dor deflate should be in the range of 0-9");
		}
	}
	
	if (parameters.isEncryptFiles()) {
		if (parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_STANDARD && 
				parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_AES) {
			throw new ZipException("unsupported encryption method");
		}
		
		if (parameters.getPassword() == null || parameters.getPassword().length <= 0) {
			throw new ZipException("input password is empty or null");
		}
	} else {
		parameters.setAesKeyStrength(-1);
		parameters.setEncryptionMethod(-1);
	}
	
}
 
Example 4
Source File: UnzipEngine.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
private void initDecrypter(RandomAccessFile raf) throws ZipException {
	if (localFileHeader == null) {
		throw new ZipException("local file header is null, cannot init decrypter");
	}
	
	if (localFileHeader.isEncrypted()) {
		if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) {
			decrypter = new StandardDecrypter(fileHeader, getStandardDecrypterHeaderBytes(raf));
		} else if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) {
			decrypter = new AESDecrypter(localFileHeader, getAESSalt(raf), getAESPasswordVerifier(raf));
		} else {
			throw new ZipException("unsupported encryption method");
		}
	}
}
 
Example 5
Source File: CipherOutputStream.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
public void write(byte[] b, int off, int len) throws IOException {
	if (len == 0) return;
	
	if (zipParameters.isEncryptFiles() && 
			zipParameters.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) {
		if (pendingBufferLength != 0) {
			if (len >= (InternalZipConstants.AES_BLOCK_SIZE - pendingBufferLength)) {
				System.arraycopy(b, off, pendingBuffer, pendingBufferLength,
								(InternalZipConstants.AES_BLOCK_SIZE - pendingBufferLength));
				encryptAndWrite(pendingBuffer, 0, pendingBuffer.length);
				off = (InternalZipConstants.AES_BLOCK_SIZE - pendingBufferLength);
				len = len - off;
				pendingBufferLength = 0;
			} else {
				System.arraycopy(b, off, pendingBuffer, pendingBufferLength,
						len);
				pendingBufferLength += len;
				return;
			}
		}
		if (len != 0 && len % 16 != 0) {
			System.arraycopy(b, (len + off) - (len % 16), pendingBuffer, 0, len % 16);
			pendingBufferLength = len % 16;
			len = len - pendingBufferLength; 
		}
	}
	if (len != 0)
		encryptAndWrite(b, off, len);
}
 
Example 6
Source File: PartInputStream.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
public PartInputStream(RandomAccessFile raf, long start, long len, UnzipEngine unzipEngine) {
    this.raf = raf;
    this.unzipEngine = unzipEngine;
    this.decrypter = unzipEngine.getDecrypter();
    this.bytesRead = 0;
    this.length = len;
    this.isAESEncryptedFile = unzipEngine.getFileHeader().isEncrypted() && 
		unzipEngine.getFileHeader().getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES;
}
 
Example 7
Source File: CipherOutputStream.java    From AndroidZip with Apache License 2.0 4 votes vote down vote up
public void closeEntry() throws IOException, ZipException {
	
	if (this.pendingBufferLength != 0) {
		encryptAndWrite(pendingBuffer, 0, pendingBufferLength);
		pendingBufferLength = 0;
	}
	
	if (this.zipParameters.isEncryptFiles() && 
			this.zipParameters.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) {
		if (encrypter instanceof AESEncrpyter) {
			outputStream.write(((AESEncrpyter)encrypter).getFinalMac());
			bytesWrittenForThisFile += 10;
			totalBytesWritten += 10;
		} else {
			throw new ZipException("invalid encrypter for AES encrypted file");
		}
	}
	fileHeader.setCompressedSize(bytesWrittenForThisFile);
	localFileHeader.setCompressedSize(bytesWrittenForThisFile);
	
	if (zipParameters.isSourceExternalStream()) {
		fileHeader.setUncompressedSize(totalBytesRead);
		if (localFileHeader.getUncompressedSize() != totalBytesRead) {
			localFileHeader.setUncompressedSize(totalBytesRead);
		}
	}
	
	long crc32 = crc.getValue();
	if (fileHeader.isEncrypted()) {
		if (fileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) {
			crc32 = 0;
		}
	}
	
	if (zipParameters.isEncryptFiles() && 
			zipParameters.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) {
		fileHeader.setCrc32(0);
		localFileHeader.setCrc32(0);
	} else {
		fileHeader.setCrc32(crc32);
		localFileHeader.setCrc32(crc32);
	}
	
	zipModel.getLocalFileHeaderList().add(localFileHeader);
	zipModel.getCentralDirectory().getFileHeaders().add(fileHeader);
	
	HeaderWriter headerWriter = new HeaderWriter();
	totalBytesWritten += headerWriter.writeExtendedLocalHeader(localFileHeader, outputStream);
	
	crc.reset();
	bytesWrittenForThisFile = 0;
	encrypter = null;
	totalBytesRead = 0;
}