Chapter 4
82
Notice that 'e' always encrypt to the exact same ciphertext. If an attacker knew that we were sending text
messages, then a frequency analysis would quickly indicate that e's were encrypted to "TRw+doCp3EQ=".
With some time and a complete encrypted session transcript, it wouldn't be difficult to crack the code, as
the use of ECB in this application has reduced the strength of the encryption from 256 bits to less than 100
characters. This is only as secure as a simple character-by-character replacement.
The real weakness here is that each block is encrypted the same way. If our key or data keeps changing,
ECB is perfectly safe. But if similar blocks keep getting sent with the same key, it is possible to gain some
information from those blocks that we might not want broadcast.
CBC (Cipher Block Chaining)
CBC mode changes the behavior of the cipher so that the same plaintext block no longer necessarily
encrypts to the same ciphertext block, thus solving the main problem with ECB. CBC uses information
from the previous block to encrypt the current block, thus changing it from ECB. A problem with this
method is that identical messages will still encrypt identically, because all of the blocks that would alter
future blocks are the same. To fix this, we need to use an initialization vector or IV. The IV is just a block
of random data used to initialize the cipher. It need not be kept secret, but it should be different for every
message. That way, even if we send two identical messages, as long as they have different IVs, they will
encrypt differently. In that sense, an initialization vector is a lot like salt used in password-based encryption.
CBC is suitable for transmitting text, but it requires transmitting a full block of data at a time usually 8
characters. This is fine for a complete message, but not for a talk application, which needs to send a single
character at a time.
CFB (Cipher FeedBack)
CFB works similarly to CBC, except that it can operate on smaller chunks of data typically 8 bits. This is
perfect for encrypting something like a chat session, where single byte chunks of data need to be sent.
CFB also requires an IV that must be unique for each message sent with the same key.
OFB (Output FeedBack)
OFB is similar to CFB, except that it provides better protection against data being lost in transit. A single
bit error in the ciphertext produces a single bit of error in the plaintext. Other modes cause the entire block
to get lost.
Like CFB and CBC, OFB also requires an IV.
CipherStreams
Some of the most useful classes that the JCE provides are the two CipherStream classes in the
javax.crypto package: CipherInputStream and CipherOutputStream. They provide convenient
wrappers around standard input and output streams that automatically encrypt and decrypt. You can use
them anywhere you use a normal InputStream or OutputStream, like network programming or file IO.
CipherInputStream and CipherOutputStream are both constructed by a call to new() with the
stream to wrap and the cipher to use. If we have a cipher prepared, we could encrypt a file with: