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

The following examples show how to use net.lingala.zip4j.util.Zip4jConstants#AES_STRENGTH_128 . 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: AESEncrpyter.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
public AESEncrpyter(char[] password, int keyStrength) throws ZipException {
	if (password == null || password.length == 0) {
		throw new ZipException("input password is empty or null in AES encrypter constructor");
	}
	if (keyStrength != Zip4jConstants.AES_STRENGTH_128 && 
			keyStrength != Zip4jConstants.AES_STRENGTH_256) {
		throw new ZipException("Invalid key strength in AES encrypter constructor");
	}
	
	this.password = password;
	this.keyStrength = keyStrength;
	this.finished = false;
	counterBlock = new byte[InternalZipConstants.AES_BLOCK_SIZE];
	iv = new byte[InternalZipConstants.AES_BLOCK_SIZE];
	init();
}
 
Example 2
Source File: CipherOutputStream.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
private AESExtraDataRecord generateAESExtraDataRecord(ZipParameters parameters) throws ZipException {
	
	if (parameters == null) {
		throw new ZipException("zip parameters are null, cannot generate AES Extra Data record");
	}
	
	AESExtraDataRecord aesDataRecord = new AESExtraDataRecord();
	aesDataRecord.setSignature(InternalZipConstants.AESSIG);
	aesDataRecord.setDataSize(7);
	aesDataRecord.setVendorID("AE");
	// Always set the version number to 2 as we do not store CRC for any AES encrypted files
	// only MAC is stored and as per the specification, if version number is 2, then MAC is read
	// and CRC is ignored
	aesDataRecord.setVersionNumber(2); 
	if (parameters.getAesKeyStrength() == Zip4jConstants.AES_STRENGTH_128) {
		aesDataRecord.setAesStrength(Zip4jConstants.AES_STRENGTH_128);
	} else if (parameters.getAesKeyStrength() == Zip4jConstants.AES_STRENGTH_256) {
		aesDataRecord.setAesStrength(Zip4jConstants.AES_STRENGTH_256);
	} else {
		throw new ZipException("invalid AES key strength, cannot generate AES Extra data record");
	}
	aesDataRecord.setCompressionMethod(parameters.getCompressionMethod());
	
	return aesDataRecord;
}
 
Example 3
Source File: UnzipEngine.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
private int calculateAESSaltLength(AESExtraDataRecord aesExtraDataRecord) throws ZipException {
	if (aesExtraDataRecord == null) {
		throw new ZipException("unable to determine salt length: AESExtraDataRecord is null");
	}
	switch (aesExtraDataRecord.getAesStrength()) {
	case Zip4jConstants.AES_STRENGTH_128:
		return 8;
	case Zip4jConstants.AES_STRENGTH_192:
		return 12;
	case Zip4jConstants.AES_STRENGTH_256:
		return 16;
	default:
		throw new ZipException("unable to determine salt length: invalid aes key strength");
	}
}
 
Example 4
Source File: AESEncrpyter.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
private void init() throws ZipException {
	switch (keyStrength) {
	case Zip4jConstants.AES_STRENGTH_128:
		KEY_LENGTH = 16;
		MAC_LENGTH = 16;
		SALT_LENGTH = 8;
		break;
	case Zip4jConstants.AES_STRENGTH_256:
		KEY_LENGTH = 32;
		MAC_LENGTH = 32;
		SALT_LENGTH = 16;
		break;
	default:
		throw new ZipException("invalid aes key strength, cannot determine key sizes");
	}
	
	saltBytes = generateSalt(SALT_LENGTH);
	byte[] keyBytes = deriveKey(saltBytes, password);
	
	if (keyBytes == null || keyBytes.length != (KEY_LENGTH + MAC_LENGTH + PASSWORD_VERIFIER_LENGTH)) {
		throw new ZipException("invalid key generated, cannot decrypt file");
	}
	
	aesKey = new byte[KEY_LENGTH];
	macKey = new byte[MAC_LENGTH];
	derivedPasswordVerifier = new byte[PASSWORD_VERIFIER_LENGTH];
	
	System.arraycopy(keyBytes, 0, aesKey, 0, KEY_LENGTH);
	System.arraycopy(keyBytes, KEY_LENGTH, macKey, 0, MAC_LENGTH);
	System.arraycopy(keyBytes, KEY_LENGTH + MAC_LENGTH, derivedPasswordVerifier, 0, PASSWORD_VERIFIER_LENGTH);
	
	aesEngine = new AESEngine(aesKey);
	mac = new MacBasedPRF("HmacSHA1");
	mac.init(macKey);
}
 
Example 5
Source File: AESDecrypter.java    From AndroidZip with Apache License 2.0 4 votes vote down vote up
private void init(byte[] salt, byte[] passwordVerifier) throws ZipException {
	if (localFileHeader == null) {
		throw new ZipException("invalid file header in init method of AESDecryptor");
	}
	
	AESExtraDataRecord aesExtraDataRecord = localFileHeader.getAesExtraDataRecord();
	if (aesExtraDataRecord == null) {
		throw new ZipException("invalid aes extra data record - in init method of AESDecryptor");
	}
	
	switch (aesExtraDataRecord.getAesStrength()) {
	case Zip4jConstants.AES_STRENGTH_128:
		KEY_LENGTH = 16;
		MAC_LENGTH = 16;
		SALT_LENGTH = 8;
		break;
	case Zip4jConstants.AES_STRENGTH_192:
		KEY_LENGTH = 24;
		MAC_LENGTH = 24;
		SALT_LENGTH = 12;
		break;
	case Zip4jConstants.AES_STRENGTH_256:
		KEY_LENGTH = 32;
		MAC_LENGTH = 32;
		SALT_LENGTH = 16;
		break;
	default:
		throw new ZipException("invalid aes key strength for file: " + localFileHeader.getFileName());
	}
	
	if (localFileHeader.getPassword() == null || localFileHeader.getPassword().length <= 0) {
		throw new ZipException("empty or null password provided for AES Decryptor");
	}
	
	byte[] derivedKey = deriveKey(salt, localFileHeader.getPassword());
	if (derivedKey == null || 
			derivedKey.length != (KEY_LENGTH + MAC_LENGTH + PASSWORD_VERIFIER_LENGTH)) {
		throw new ZipException("invalid derived key");
	}
	
	aesKey = new byte[KEY_LENGTH];
	macKey = new byte[MAC_LENGTH];
	derivedPasswordVerifier = new byte[PASSWORD_VERIFIER_LENGTH];
	
	System.arraycopy(derivedKey, 0, aesKey, 0, KEY_LENGTH);
	System.arraycopy(derivedKey, KEY_LENGTH, macKey, 0, MAC_LENGTH);
	System.arraycopy(derivedKey, KEY_LENGTH + MAC_LENGTH, derivedPasswordVerifier, 0, PASSWORD_VERIFIER_LENGTH);
	
	if (derivedPasswordVerifier == null) {
		throw new ZipException("invalid derived password verifier for AES");
	}
	
	if (!Arrays.equals(passwordVerifier, derivedPasswordVerifier)) {
		throw new ZipException("Wrong Password for file: " + localFileHeader.getFileName(), ZipExceptionConstants.WRONG_PASSWORD);
	}
	
	aesEngine = new AESEngine(aesKey);
	mac = new MacBasedPRF("HmacSHA1");
	mac.init(macKey);
}