One way to avoid some of those problems is to use Cipher Block Chaining.
The idea here is that we use the ciphertext from the previous block
to impact the next block. So here's what this looks like.
So we're still going to break our message into blocks.
So this is the idea of Cipher Block Chaining.
We're going to take each message block, encrypt it,
with our encryption function, although let's assume it's still AES,
using the same key, so we're using the same key for each block.
We're going to get as output a cipher text.
Instead of doing each block independently, though,
and having the codebook property, for the second block, we're going to take
the ciphertext that came out for the first block, EXOR that with the message block,
and then make that the input term of the encryption function.
So this keeps going. This means, as a result,
in CBC or Cipher Block Chaining mode,
the (i)th encrypted block is the result of encrypting the EXOR
of the (i)th message block with the (i-1)th encryption block.
We have a little bit of an issue with the first one.
The first one, well there's no 0th block. If we just did what was shown here,
well then we'd still have a problem that we would see repetition
every time the first block in a file is the same as the first block in another file,
encrypted with the same key.
We'd get the same C0 out.
So we don't want that. We're going to add what's called an "initialization vector,"
and we'll EXOR that with the first message. That keeps things consistent
--each message is being EXOR'd with something before it is encrypted--
and this might worry us--that we're adding more secrets--
we want to minimize the number of secrets--to be as few as possible--
the IV doesn't really need to be kept secret. Note that we're still
encrypting this output. It's helpful to not reuse an IV, though.
So it's OK to make the IV something unsecret,
as long as it's not always the same.
