# Keystream

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Keystream
> Markdown URL: https://mediated.wiki/source/Keystream.md
> Source: https://en.wikipedia.org/wiki/Keystream
> Source revision: 1136438276
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Concept in cryptography}}
In [cryptography](/source/cryptography), a '''keystream''' is a [stream](/source/Stream_(computing)) of [random](/source/Randomness) or [pseudorandom](/source/Pseudorandomness) characters that are combined with a [plaintext](/source/plaintext) message to produce an encrypted message (the ciphertext).

The "characters" in the keystream can be [bit](/source/bit)s, [byte](/source/byte)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](/source/bitwise_XOR) with a character in the plaintext to produce the ciphertext, using [modular arithmetic](/source/modular_arithmetic).

Keystreams are used in the [one-time pad](/source/one-time_pad) cipher and in most [stream cipher](/source/stream_cipher)s. [Block cipher](/source/Block_cipher)s can also be used to produce keystreams. For instance, CTR mode is a [block mode](/source/Block_cipher_modes_of_operation) 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](/source/modular_arithmetic).)

Here the plaintext message "attack at dawn" is combined by addition with the keystream "kjcngmlhylyu" and produces the ciphertext "kcvniwlabluh".

{| class="wikitable" 
|- align=center
! Plaintext 
| a || t || t || a || c || k || a || t || d || a || w || n
|- align=right
! Plaintext as numbers 
| 0  || 19 || 19 || 0  || 2  || 10 || 0  || 19 || 3  || 0  || 22 || 13
|- align=center
! Keystream
|  k ||  j ||  c ||  n ||  g ||  m ||  l ||  h ||  y ||  l ||  y ||  u
|- align=right
! Keystream as numbers
| 10 ||  9 ||  2 || 13 ||  6 || 12 || 11 ||  7 || 24 || 11 || 24 || 20
|- align=right
! Ciphertext as numbers 
| 10 || 28 || 21 || 13 ||  8 || 22 || 11 || 26 || 27 || 11 || 46 || 33
|- align=right
! Ciphertext as numbers <br> wrapped to 0-25 
| 10 ||  2 || 21 || 13 ||  8 || 22 || 11 ||  0 ||  1 || 11 || 20 ||  7
|- align=center
! Ciphertext as text 
|  k ||  c ||  v ||  n ||  i ||  w ||  l ||  a ||  b ||  l ||  u ||  h
|}

== References ==

* ''[http://www.cacr.math.uwaterloo.ca/hac/ Handbook of Applied Cryptography]'' by Menezes, van Oorschot and Vanstone (2001), chapter 1, 6 and 7.
<references/>

{{cryptography navbox|stream}}

Category:Stream ciphers

---
Adapted from the Wikipedia article [Keystream](https://en.wikipedia.org/wiki/Keystream) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Keystream?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
