The wonderful safe travel DES algorithm (2)

Hi, everyone. In the previous section, we introduced the basic content of the symmetric encryption algorithm DES in detail. Since the length of the plaintext is not fixed, and the encryption algorithm can only process a piece of data of a specific length, it is necessary to compare the longer plaintext for grouping Re-encryption, but after grouping, the length of the last group may be insufficient, so the last group of messages needs to be filled according to the filling mode.

Symmetrically encrypted packet mode

There are five grouping modes for symmetric encryption, let's introduce them below.

1. ECB-Electronic Code Book, electronic code book mode

Advantages :

  • Simple and efficient
  • Conducive to parallel computing
  • Errors will not be transmitted

Disadvantages :

  • The ciphertext is regular and easy to crack

  • Possible active attacks on plaintext

Features :

  • The last plaintext group must be filled
    • des/3des -> the last packet is filled with 8 bytes
    • aes -> the last packet is filled with 16 bytes
  • No need for initialization vector

ECB

2. CBC-Cipher Block Chaining, cipher block chain mode (recommended)

Advantages :

  • Not easy to be attacked, good security
  • Suitable for transmitting long messages
  • It is the standard of SSL and IPSec

Disadvantages :

  • Not conducive to parallel computing
  • Error will pass
  • Need initialization vector

Features :

  • The last plaintext group needs to be filled
    • des/3des -> the last packet is filled with 8 bytes
    • aes -> the last packet is filled with 16 bytes
  • Need an initialization vector-an array
    • The length of the array: equal to the plaintext grouping
    • Data source: provided by the person responsible for encryption
    • The initialization vector value used for encryption and decryption must be the same

CBC

3. CFB-Cipher FeedBack, ciphertext feedback mode

Advantages :

  • The plaintext mode is hidden, and the ciphertext is irregular
  • Convert block cipher to stream mode
  • Can encrypt and transmit data smaller than packets in time

Disadvantages :

  • Not conducive to parallel computing
  • Error propagation: a single damage to a plaintext affects multiple units
  • Need initialization vector

Features : Plaintext grouping is a bitwise XOR operation with a data stream, and finally ciphertext is generated

  • Need an initialization vector-an array
    • The length of the array: equal to the plaintext grouping
    • Data source: provided by the person responsible for encryption
    • The initialization vector value used for encryption and decryption must be the same
  • No need to fill

CFB

4. OFB-Output-Feedback, output feedback mode

advantage:

  • Plaintext mode hidden
  • Convert block cipher to stream mode
  • Can encrypt and transmit data smaller than packets in time

Disadvantages:

  • Not conducive to parallel computing
  • Active attack on plaintext is possible
  • Error transmission: damage to one plaintext unit affects multiple units

Features: The ciphertext is irregular, the plaintext grouping is a bitwise XOR operation with a data stream, and the ciphertext is finally generated

  • Need an initialization vector-an array
    • The length of the array: equal to the plaintext grouping
    • Data source: provided by the person responsible for encryption
    • The initialization vector value used for encryption and decryption must be the same
  • No need to fill

OFB

5. CTR-CounTeR, counter mode (emphasis, recommended)

Features :

  • The ciphertext is irregular, and the plaintext grouping is a bitwise XOR operation with a data stream, and finally the ciphertext is generated
  • No need for initialization vector
  • No need to fill

CTR

Among the above five grouping modes, the ECB mode is easy to crack and is rarely used anymore. The other four grouping modes have their own advantages.
However, CBC mode and CTR mode are highly recommended, especially CTR mode, which does not require filling, and the code is very convenient to implement. Moreover, the encryption and decryption methods are the same, and concurrent grouping can be realized, with high efficiency and security.

About vectors in CBC mode:

In the CBC (not only the DES algorithm) mode, the vector iv is generated by a random number (or pseudo-random) mechanism is a relatively common method. The function of iv is mainly used to generate the first block of ciphertext, so that the final generated ciphertext will be different (in the case of the same plaintext), making password attacks more difficult, except for iv. use. The biggest advantage is that even the same plaintext and the same key can produce different ciphertexts.

Filling mode of symmetric encryption

There are generally four common types of data filling, namely:

  • NoPadding
  • PKCS5Padding (PKCS7Padding)
  • Zero Padding
  • ISO 10126 Padding
NoPadding

The API or algorithm itself does not process the data, and the encrypted data shall be filled with the algorithm agreed upon by both parties. For example, if you encrypt and decrypt string data, you can add \0 or spaces, and trim the data after decryption.

PKCS5Padding (PKCS7Padding)

PKCS5Padding and PKCS7Padding are the default padding methods in Java and .Net respectively. PKCS5Padding and PKCS7Padding are actually different in terms of protocol. According to relevant information, PKCS5Padding clearly defines the encryption block to be 8 bytes, and the PKCS7Padding encryption block can be between 1-255 . But the encapsulated DES algorithms are all 8 bytes by default, so they can be considered the same. Data supplementation is actually a filling process of supplementing to a multiple of 8 bytes when the data is less than a multiple of 8 bytes.

Before encryption : the length of the data byte is the remainder of 8, and the remainder is m. If m>0, then make up 8-m bytes, and the byte value is 8-m, that is, make up a few bytes if it is a few bytes away , The byte value is the number of supplemented bytes, if it is 0, it will supplement 8 bytes of 8

After decryption : take the last byte and the value is m, then delete m bytes from the end of the data, and the remaining data is the original text before encryption

For example: if the encrypted string is AAA, the difference is 5 bytes, then the complement is AAA55555; the encrypted string is BBBBBB, and the difference is 2 bytes, then the complement is BBBBBB22; the encrypted string is CCCCCCCC, the difference is 0 bytes , The complement is CCCCCCCC88888888.

Zero Padding

0 padding, as the name implies, is that all less than 8 bits are filled with 0, but the 0 padding protocol is not standardized in the encryption algorithm, and 0 padding may be problematic. When the plain text itself has one or more 0 bytes ending, it is very It is difficult to distinguish whether it is a padded 0 or the original message.

For example: the following is an encrypted block with 8 bytes as the unit, when the last four bytes are not enough, use 0 to fill

… | DD DD DD DD DD DD DD DD | DD DD DD DD 00 00 00 00 |

ISO 10126

The filling mode of ISO 10126 defines that the bytes before the last byte of the message can be filled randomly, and the total number of supplemented bytes is filled in the last byte.

For example: In the following example, a total of 4 bytes need to be added at the end, the first three bytes are randomly filled bytes, and the last byte needs to be filled with 4, which means that the padding is filled with 4 bytes in total.

… | DD DD DD DD DD DD DD DD | DD DD DD DD 81 A6 23 04 |

When does it need to be filled, and when does it need to be filled?

Observing the icon of the packet mode, we can see that the operation or operation does not require padding after encryption, while the advanced or operation re-encryption does not require padding. This is because the operation requires two data of the same length. Compare and calculate one by one!

to sum up

Among the above five grouping modes, the ECB mode is easy to crack and is rarely used anymore. The other four grouping modes have their own advantages.

But the CBC mode and CTR mode are highly recommended, especially the CTR mode, which does not require filling, and the code is very convenient to implement. Moreover, the encryption and decryption methods are the same, and concurrent grouping can be realized, with high efficiency and security.

Reference article :

1. Five grouping modes commonly used in symmetric encryption algorithms (ECB/CBC/CFB/OFB/CTR)

2. The effect of DES filling method and initial vector IV

3. Several encryption modes and filling methods of DES algorithm

Welcome everyone to follow the "I am a developer FTD" official account, WeChat account: ForTheDevelopers

Also welcome everyone to add my personal WeChat exchange, WeChat ID: ForTheDeveloper

Pay attention to development, pay more attention to developers!

Guess you like

Origin blog.csdn.net/ForTheDevelopers/article/details/112727014