package aalg.crypto;

public class DESDecryptor extends DESBase implements IBlockCipherTransformation{

	private final DES cipher;
	public DESDecryptor(DES cipher) {
		this.cipher = cipher;
	}
	
	@Override
	public IBlockCipher getCipher() {
		return cipher;
	}

	@Override
	public void transformBlock(byte[] sourceBlock, int sourceOffset,
			byte[] targetBlock, int targetOffset) {
		
		if (sourceBlock.length - sourceOffset < (DES.DATA_LENGTH))
			throw new RuntimeException("too big offset");
			
		
		long newBlock = 0;					
		newBlock = byteArray_to_long(sourceBlock, sourceOffset, DES.DATA_LENGTH);	
		
		newBlock = feistel_deciphering(newBlock);
		
		for(int i = 0; i< DES.DATA_LENGTH ; i++)
		{
			 targetBlock[i] = (byte)((newBlock >> 8*i) & 0xFF) ;
			 targetBlock[i] = (byte)((Integer.reverse ((int)targetBlock[i])) >>> 24);
		}
				
	}

	@Override
	public void setKey(ISymmetricKey key) {
		
		ByteArraySymmetricKey byteKey = (ByteArraySymmetricKey) key;		
		
		if (byteKey.getSize() != DES.KEY_SIZE+1)
			throw new RuntimeException("wrong key size");
		
		long key1 = 0;
		
		key1 = byteKey_to_long(byteKey, DES.KEY_SIZE+1);
		
		calculateRoundKeys(key1);
		
	}

}
