Keystream

Keystream

Discussion

Encyclopedia
In cryptography
Cryptography
Cryptography is the practice and study of techniques for secure communication in the presence of third parties...

, a keystream is a stream of random
Randomness
Randomness has somewhat differing meanings as used in various fields. It also has common meanings which are connected to the notion of predictability of events....

or pseudorandom
Pseudorandomness
A pseudorandom process is a process that appears to be random but is not. Pseudorandom sequences typically exhibit statistical randomness while being generated by an entirely deterministic causal process...

characters that are combined with a plaintext
Plaintext
In cryptography, plaintext is information a sender wishes to transmit to a receiver. Cleartext is often used as a synonym. Before the computer era, plaintext most commonly meant message text in the language of the communicating parties....

message to produce an encrypted message (the ciphertext).

The "characters" in the keystream can be bit
Bit
A bit is the basic unit of information in computing and telecommunications; it is the amount of information stored by a digital device or other physical system that exists in one of two possible distinct states...

s, byte
Byte
The byte is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, a byte was the number of bits used to encode a single character of text in a computer and for this reason it is the basic addressable element in many computer...

s, numbers or actual characters like A-Z depending on the usage case.

Usually each character in the keystream is either added, subtracted or XORed with a character in the plaintext to produce the ciphertext, using modular arithmetic
Modular arithmetic
In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" after they reach a certain value—the modulus....

.

Keystreams are used in the one-time pad
In cryptography, the one-time pad is a type of encryption, which has been proven to be impossible to crack if used correctly. Each bit or character from the plaintext is encrypted by a modular addition with a bit or character from a secret random key of the same length as the plaintext, resulting...

cipher and in most stream cipher
Stream cipher
In cryptography, a stream cipher is a symmetric key cipher where plaintext digits are combined with a pseudorandom cipher digit stream . In a stream cipher the plaintext digits are encrypted one at a time, and the transformation of successive digits varies during the encryption...

s. Block cipher
Block cipher
In cryptography, a block cipher is a symmetric key cipher operating on fixed-length groups of bits, called blocks, with an unvarying transformation. A block cipher encryption algorithm might take a 128-bit block of plaintext as input, and output a corresponding 128-bit block of ciphertext...

s can also be used to produce keystreams. For instance, CTR mode is a block mode
Block cipher modes of operation
In cryptography, modes of operation is the procedure of enabling the repeated and secure use of a block cipher under a single key.A block cipher by itself allows encryption only of a single data block of the cipher's block length. When targeting a variable-length message, the data must first be...

that makes a block cipher produce a keystream and thus turns the block cipher into a stream cipher.

Example

In this simple example we use the English alphabet of 26 characters from a-z. Thus we can not encrypt numbers, commas, spaces and other symbols. The random numbers in the keystream then have to be at least between 0 and 25.

To encrypt we add the keystream numbers to the plaintext. And to decrypt we subtract the same keystream numbers from the ciphertext to get the plaintext.

If a ciphertext number becomes larger than 25 we wrap it to a value between 0-25. Thus 26 becomes 0 and 27 becomes 1 and so on. (Such wrapping is called modular arithmetic
Modular arithmetic
In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" after they reach a certain value—the modulus....

.)

Here the plaintext message "attack at dawn" is combined by addition with the keystream "kjcngmlhylyu" and produces the ciphertext "kcvniwlabluh".
 Plaintext Plaintext as numbers Keystream Keystream as numbers Ciphertext as numbers Ciphertext as numbers wrapped to 0-25 Ciphertext as text a t t a c k a t d a w n 0 19 19 0 2 10 0 19 3 0 22 13 k j c n g m l h y l y u 10 9 2 13 6 12 11 7 24 11 24 20 10 28 21 13 8 22 11 26 27 11 46 33 10 2 21 13 8 22 11 0 1 11 20 7 k c v n i w l a b l u h