All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nervousync.commons.zip.crypto.impl.AESEncryptor Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nervousync.commons.zip.crypto.impl;

import com.nervousync.commons.core.Globals;
import com.nervousync.commons.core.zip.ZipConstants;
import com.nervousync.commons.zip.crypto.Encryptor;
import com.nervousync.commons.zip.operator.RawOperator;
import com.nervousync.exceptions.zip.ZipException;

/**
 * Encryptor implement of AES
 * @author Steven Wee	[email protected]
 * @version $Revision: 1.0 $ $Date: Nov 30, 2017 3:42:11 PM $
 */
public class AESEncryptor extends AESCrypto implements Encryptor {
	
	private boolean finished = Globals.DEFAULT_VALUE_BOOLEAN;
	
	public AESEncryptor(char[] password, int aesStrength) throws ZipException {
		super.preInit(aesStrength);
		this.init(password);
	}

	@Override
	public int encryptData(byte[] buff) throws ZipException {
		if (buff == null) {
			throw new ZipException("input bytes are null, cannot perform AES encrpytion");
		}
		return this.encryptData(buff, 0, buff.length);
	}

	@Override
	public int encryptData(byte[] buff, int start, int len) throws ZipException {
		if (this.finished) {
			throw new ZipException("AES Encrypter is in finished state (A non 16 byte block has already been passed to encrypter)");
		}
		
		if (len % 16 != 0) {
			this.finished = true;
		}
		
		for (int i = start ; i < (start + len) ; i += ZipConstants.AES_BLOCK_SIZE) {
			this.loopCount = (i + ZipConstants.AES_BLOCK_SIZE <= (start + len)) ? 
					ZipConstants.AES_BLOCK_SIZE : ((start + len) - i);
			this.iv = RawOperator.prepareAESBuffer(this.nonce);
			this.aesEngine.processBlock(this.iv, this.countBlock);
			
			for (int j = 0 ; j < this.loopCount ; j++) {
				buff[i + j] = (byte)(buff[i + j] ^ this.countBlock[j]);
			}
			
			this.macBasedPRF.update(buff, i, this.loopCount);
			this.nonce++;
		}
		
		return len;
	}
	
	public byte[] getFinalMac() {
		byte[] rawMacBytes = this.macBasedPRF.doFinal();
		byte[] macBytes = new byte[10];
		System.arraycopy(rawMacBytes, 0, macBytes, 0, 10);
		return macBytes;
	}
	
	/**
	 * @return the derviedPasswordVerifier
	 */
	public byte[] getDerviedPasswordVerifier() {
		return derviedPasswordVerifier;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy