Technology Sharing

【Cryptology】Basic concepts of stream ciphers

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Before introducing stream ciphers, let's first understand a basic prerequisite knowledge point - XOR operation.

1. XOR

Operation rules: The same is 0, different is 1

Features: A bit can be restored to its original state by performing two XOR operations on it.

Plain text: 1100

Key: 0101 (encrypted)

Ciphertext: 1001

Key: 0101 (decryption)

Plain text: 1100

advantage:This reversibility allows the XOR operation to be used for both encryption and decryption transformations.

shortcoming:If an attacker can guess or capture the key, it can also be easily decrypted via XOR.

The reason why the XOR operation can be used for both encryption and decryption is its reversibility, but this is also its disadvantage. Its security depends entirely on key hiding.

2. One-time pad (OTP) algorithm

(1) Definition

One-Time Pad (OTP) is a stream cipher algorithm that is considered to be one of the most secure encryption methods in theory, provided that its implementation strictly follows several key principles:

1.  The key must be as long as the plaintext: This means that each encryption uses a key that is exactly the same length as the information to be encrypted.

2.  The key must be truly random: Each bit of the key should be randomly generated without any pattern or predictability.

3.  The key must be used only once: The same key must never be used to encrypt multiple messages, otherwise the correlation between the ciphertexts can be used to infer the content of the messages.

4.  The key must be kept secret: The distribution and storage of keys must be extremely secure to prevent them from being obtained by third parties.

(2) Advantages and disadvantages

advantage:

  • The key is randomly generated and used only once
  • Unconditional safety
  • Encryption and decryption are addition operations, which are more efficient.

shortcoming:

  • The key length is at least as long as the plaintext length, making key sharing difficult and not very practical.

3. Definition of stream cipher

When people were studying one-time encryption algorithms, they tried to solve the problems of key management and length. If there was a way to generate all the keys for encrypting plaintext by providing only a small key, then stream ciphers were developed based on this idea.

(1) Basic idea of ​​stream cipher

        In a stream cipher, a small key (often called a seed or initialization vector) is used to generate a pseudo-random key stream of the same length as the plaintext through a pseudo-random number generator (PRNG). This key stream is then XORed with the plaintext to produce the ciphertext. Similarly, the decryption process is to XOR the ciphertext with the same key stream to recover the plaintext.

Key kGenerate a key stream z=z_0z_1z_2...And use the following rules to x=x_0x_1x_2...To encrypt:
y=y_0y_1y_2...=Ez_0(x_0)Ez_1(x_1)Ez_2(x_2)...

Generally, a linear feedback shift register is used to generate a pseudo-random key. The principle will not be elaborated here.

(2) Algorithm process

The encryption and decryption process can be described as follows:

  • Keystream generation: Both encryption and decryption use the same key and initialization vector (IV) to initialize a pseudo-random number generator (PRNG). The PRNG generates a series of pseudo-random bits based on its internal state, which constitute the keystream.
  • Encryption process: The sender performs a bitwise XOR operation on the generated key stream and the plaintext to obtain the ciphertext. Due to the nature of the XOR operation, the same operation is reversible on different bits.
  • Decryption process: The receiver reinitializes the PRNG with the same key and IV and generates the same key stream as used for encryption. The receiver then XORs the ciphertext with the key stream to recover the original plaintext.

(3) Stream Cipher Design Principles

The design principles of stream ciphers are really focused on creating a keystream generator that can produce a keystream with certain security properties. The keystream sequence should have the following properties:

  1. The huge cycle: The period of the keystream should be long enough to prevent the reuse of the same keystream, which would expose the pattern of the encrypted data and enable cryptanalysts to attack by comparing the similarities and differences between different messages. In theory, for an n-bit key space, the ideal period length should be 2n−1. In practice, a longer period means a lower frequency of keystream repetition, which increases the security of the cryptographic system.

  2. Good statistical properties: The keystream should look like a truly random sequence of bits, meaning that it should satisfy various statistical tests, such as equal distribution of 0s and 1s, independence between any two or more consecutive bits, and the absence of predictable patterns or periodicity. Good statistical properties help ensure the unpredictability of the keystream, which is an important component of the security of a stream cipher.

  3. Anti-Linear Analysis: A stream cipher should be resistant to linear analysis, where a cryptanalyst attempts to recover the key or plaintext by finding linear correlations between the keystream and the plaintext or ciphertext. This usually requires that the output of the keystream generator is nonlinear, or at least contains enough nonlinear components to prevent simple linear equation solving methods from inferring the keystream or the key itself.