[Principle of Cryptography] Stream Cipher and RC4 Algorithm

Stream cipher

The key is input to a pseudo-random number generator , the pseudo-random number generator generates a string of random 8 digits, and each byte of the output key stream and the plaintext stream are XORed to get a byte , The same pseudo-random sequence is used for decryption.

Pay attention to the design of stream cipher:

  • The period of the encryption sequence is long. The function used by the pseudo-random number generator generates a deterministic bit stream that will eventually repeat. The longer the repetition period, the more difficult the cryptanalysis.
  • The key stream should be as close as possible to the characteristics of the true random number stream. The better the random characteristics of the key stream and the more random the cipher text, the more difficult the cryptanalysis.
  • The output of the pseudo-random number generator in the above figure is adjusted by the input key K. In order to prevent brute force attacks, the key should be long enough, currently at least 128 bits.

The advantage of block cipher is that the key can be reused, but if the stream cipher is encrypted with the same key for two civilizations, then the ciphertext analysis is quite easy.

RC4 algorithm

RC4 is a stream cipher designed by Ron Rivest for RSA in 1987. The key length is variable and byte-oriented operation. Analysis shows that the cycle of the password is likely to be greater than 10^{100}that, each output of a byte of the result only requires 8 to 16 machine operation instructions, and the software implementation is also very fast. Applied to web browsers and servers, students developed SSL/TLS standards.

The following is the key generation process of the stream cipher RC4.

In the initial state, S is the padding from 0 to 255, and T is the repeated padding of the key K (length keylen).

Perform initial replacement of S that has been initially filled. The replacement process is to exchange the i-th bit and the j-th bit. The algorithm is

j=0;
for i=0 to 255 do
    j=(j+S[i]+T[i]) mod 256;
swap(S[i],T[i]);

After completing the initial replacement of S, the key entered is no longer used, and S is replaced.

The key stream generation process is shown in the figure above, and the algorithm is

When encrypting, the value of k is XORed with the next byte of the plaintext, and when decrypting, the value of k is XORed with the next byte of the ciphertext.

Guess you like

Origin blog.csdn.net/m0_50984266/article/details/108905255