package aalg.crypto;

/**
 * Cipher-block chaining mode of block cipher operation.
 * @author Ing. Petr Ležák
 */
public class CbcStreamCipher implements ISymmetricStreamCipher {

	private final IBlockCipher blockCipher;
	private final IPadding padding;
	private final IRandomGenerator rng;
	
	public CbcStreamCipher(final IBlockCipher blockCipher, final IPadding padding, final IRandomGenerator rng) {
		this.blockCipher = blockCipher;
		this.padding = padding;
		this.rng = rng;
	}

	@Override
	public CbcStreamEncryptor getEncryptor() {
		return new CbcStreamEncryptor(blockCipher.getEncryptor(), padding.createPaddingWriter(), rng);
	}

	@Override
	public CbcStreamDecryptor getDecryptor() {
		return new CbcStreamDecryptor(blockCipher.getDecryptor(), padding.createPaddingReader());
	}
	
	/**
	 * Returns required size of symmetric key in bytes. 
	 * @return key size in bytes
	 */
	@Override
	public int getKeySize() {
		return blockCipher.getKeySize();
	}

}
