package aalg.crypto;

public class TDESEncryptor extends DESBase implements IBlockCipherTransformation{

	private final TDES cipher;
	ISymmetricKey key1, key2, key3;
	
	public TDESEncryptor(TDES cipher) {
		this.cipher = cipher;
	}


	@Override
	public IBlockCipher getCipher() {
		return cipher;
	}

	@Override
	public void transformBlock(byte[] sourceBlock, int sourceOffset,
			byte[] targetBlock, int targetOffset) {
		
		DES desCipher = new DES();
		DESEncryptor enc = new DESEncryptor(desCipher);
		DESDecryptor dec = new DESDecryptor(desCipher);
		
		enc.setKey(key1);
		enc.transformBlock(sourceBlock, sourceOffset, targetBlock, targetOffset);
		
		dec.setKey(key2);
		dec.transformBlock(targetBlock, sourceOffset, sourceBlock, targetOffset);
		
		enc.setKey(key3);
		enc.transformBlock(sourceBlock, sourceOffset, targetBlock, targetOffset);
		
	}

	@Override
	public void setKey(ISymmetricKey key) {
		throw new RuntimeException("TDESEncryptor needs 3 keys. Use setKeys instead.");
		
	}
	
	public void setKeys(ISymmetricKey key1, ISymmetricKey key2, ISymmetricKey key3)
	{
		this.key1 = key1;
		this.key2 = key2;
		this.key3 = key3;
	}
	
}
