package aalg.crypto;

public class TDESDecryptor extends DESBase implements IBlockCipherTransformation{	
	
	private final TDES cipher;
	ISymmetricKey key1, key2, key3;
	
	public TDESDecryptor(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);
		
		dec.setKey(key3);
		dec.transformBlock(sourceBlock, sourceOffset, targetBlock, targetOffset);
		
		enc.setKey(key2);
		enc.transformBlock(targetBlock, sourceOffset, sourceBlock, targetOffset);
		
		dec.setKey(key1);
		dec.transformBlock(sourceBlock, sourceOffset, targetBlock, targetOffset);
		
	}

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