# How many bytes of the last keystream block are used to XOR the final block of partial plaintext in CFB, OFB & CTR modes?

I apologize if this is a dumb question but I'm trying to understand how the final block of partial plaintext is XORed using only n number of bytes in the final keystream block.

How do we determine the number of bytes in the keystream that are required to produce a final block of the same size (128 bit) as the others?

• They can all handle any number of bits. Why do you have a concept of a block of partial plaintext here? Are you using a padding scheme, not mentioned in the question? – Squeamish Ossifrage Apr 17 '19 at 16:00
• Hi @SqueamishOssifrage I was reading Wikipedia and it says "The last partial block of plaintext is XORed with the first few bytes of the last keystream block, producing a final ciphertext block that is the same size as the final partial plaintext block." and I'm looking for a more in-depth explanation of how this works. – onetruekey Apr 17 '19 at 16:12
• You just take as many bits as you need? For, e.g., AES-CTR to encrypt an 87-bit message, you use AES to generate a 128-bit block and then take the first 87 bits to xor with your plaintext, and discard the remaining 41. – Squeamish Ossifrage Apr 17 '19 at 16:14

Suppose a block is $$b$$ bits long, say $$b = 128$$, and the message is $$\ell$$ bits long, say $$\ell = 187$$. Then the final plaintext block is $$n = \ell - \lfloor\ell/b\rfloor \cdot b$$ bits long. So use $$n$$ bits of the final keystream block; in this example, $$n = 59$$.