Since (see Binary Operations), the receiver deciphers by XOR-ing the ciphertext with the same keystream. The whole security of a stream cipher depends on how the keystream is made.
The Vigenère cipher works in a similar way with letters. It adds a keyword to the plaintext one letter at a time, so you can think of its keystream as the keyword written over and over. (In this course we call Vigenère a stream cipher for that reason. Many books call it a polyalphabetic cipher instead.)
A Binary Vigenère Cipher¶
Let’s do the same thing with bits. Encipher the plaintext RIVER with the keyword delta. First, write both in 8-bit ASCII. The keyword’s bits become the keystream, and each plaintext bit is XOR-ed with the keystream bit below it:
plaintext: R I V E R
01010010 01001001 01010110 01000101 01010010
keystream: d e l t a
⊕ 01100100 01100101 01101100 01110100 01100001
-----------------------------------------------
ciphertext: 00110110 00101100 00111010 00110001 00110011
54 44 58 49 51
6 , : 1 3The ciphertext is 6,:13. To decipher, XOR the ciphertext with the same keystream, delta, and RIVER comes back. Here is the same work in Python, using the ^ operator on one character code at a time:
plaintext = 'RIVER'
keyword = 'delta'
ciphertext = ''
for p, k in zip(plaintext, keyword):
ciphertext += chr(ord(p) ^ ord(k))
print(ciphertext)
deciphered = ''
for c, k in zip(ciphertext, keyword):
deciphered += chr(ord(c) ^ ord(k))
print(deciphered)6,:13
RIVER
Unprintable ciphertext¶
Change the plaintext to lowercase, river, and something odd happens. The ciphertext codes are 22, 12, 26, 17 and 19, all control characters, so printing the ciphertext shows nothing useful. As on the Converting Text to Binary page, use ascii to see what’s really there:
ciphertext = ''
for p, k in zip('river', 'delta'):
ciphertext += chr(ord(p) ^ ord(k))
print(ciphertext)
print(ascii(ciphertext))
'\x16\x0c\x1a\x11\x13'
The ciphertext is fine. It just isn’t printable. This happens all the time with binary ciphers, which is one reason we often show ciphertext as bits or numbers instead of characters.
The Goal: A One-Time Pad¶
A repeating keyword makes a weak keystream, for the same reasons the Vigenère cipher is weak. The ideal keystream is the one from the one-time pad: truly random bits, as long as the message, and never used again. A stream cipher with a keystream like that can’t be broken.
The trouble is sharing it. The sender and receiver need exactly the same keystream, bit for bit, and a truly random keystream is as long as all the messages it will ever protect. Getting that much key to the receiver safely is as hard as sending the messages.
Pseudorandom Number Generators¶
The practical answer is a pseudorandom number generator (PRNG).
So the sender and receiver only need to share the seed, and each can make the same keystream. The seed is the key.
In the diagram below, the sender and the receiver each turn the same seed into the same keystream. The sender XORs it with the plaintext, and the receiver XORs it with the ciphertext to get the plaintext back.
Python’s random module is a PRNG. The function random.getrandbits(n) returns an -bit random integer, and random.seed sets the seed. Here is an example that enciphers Go! with seed 2718:
import random
message = 'Go!'
plaintext = text_to_integer(message)
num_bits = 8 * len(message)
random.seed(2718)
keystream = random.getrandbits(num_bits)
ciphertext = plaintext ^ keystream
print(plaintext, keystream, ciphertext)4681505 3644289 7402656
The receiver, who knows the seed and the number of bits, makes the same keystream and XORs again:
random.seed(2718)
keystream = random.getrandbits(num_bits)
integer_to_text(ciphertext ^ keystream)'Go!'Both numbers have to be right. With the same seed, random.getrandbits(24) and random.getrandbits(32) give completely different keystreams.
random versus secrets¶
Python also has a secrets module, whose secrets.randbits(n) makes random bits that are much harder to predict. But secrets has no seed, so the receiver can never make the same keystream again. That makes it useless for this kind of stream cipher, although it is the right tool for making a random key or seed in the first place.
The random module has the opposite problem. Its output is reproducible, which is what a stream cipher needs, but it was built for simulations and games, not for secrecy. Python’s documentation warns that random should not be used for security. And since the seed is the key, an attacker doesn’t need to guess the keystream at all. They only need to guess the seed. If you choose a seed between 1 and 1,000, there are only 1,000 keys to try, no matter how long and random-looking the keystream is.
A stream cipher is only as good as its keystream generator. The rest of this chapter looks at a keystream generator that is easy to build in hardware, the linear feedback shift register, and at what happens when attackers go after it.
Where Stream Ciphers Are Used¶
Stream ciphers are fast and simple to build into hardware. They also work well when data arrives in pieces of unknown length, such as a phone call or a wireless connection, since each bit can be enciphered as soon as it arrives. Some well-known examples:
A5/1 protected GSM (2G) mobile phone calls. It combines three LFSRs. See LFSR Sum.
CSS protected movies on DVDs. It combines two LFSRs. See The CSS Cipher.
WEP was an early Wi-Fi security standard that used a stream cipher called RC4.
ChaCha20 is a modern stream cipher. It is one of the ciphers used to protect web traffic today.
The first three have all been broken. ChaCha20 is still considered secure.