Zero, terminology introduction

Two common verbs:

  • Encryption: cipher or encrypt
  • Decryption: decipher or decrypt

There are several other terms that need explanation:

  • cipher: refers to the "cipher algorithm" used for encryption and decryption, and is sometimes directly translated into "password"
  • cryptographic algorithm: cryptographic algorithm, generally refers to various algorithms related to cryptography
  • ciphertext: ciphertext, that is, encrypted information. The corresponding word is plaintext
  • Password: This should not need explanation. It is the various character or numeric passwords we use daily, which can also be called passwords. They are usually relatively short (most user passwords should only be 8-16 characters).
    • Because there are too many sites and passwords are too difficult to remember, modern society is gradually recommending the use of biometrics (fingerprints, facial recognition, etc., such as pass keys, mobile phone fingerprint recognition) or hardware keys (U2F) to replace traditional passwords.
  • passphrase : Translated into "password phrase", which is a Password composed of words. It is characterized by its relatively long length and is easier to remember than a random password.
    • If you have used ssh/gnupg/openssl and other tools to generate or use keys, you should be familiar with them. Their passphrase length is not limited, so you can use a you-are-not-my-enemy-but-I'm-your-fatherphrase like this as the password, which is strong and easy to remember.

In cryptography, the most easily confused word is probably "password". cipher/password/passphrase can all be translated into "password". You need to pay attention to the difference.


1. What is symmetric encryption?

In cryptography, there are two encryption schemes that are widely used: "symmetric encryption" and "asymmetric encryption".

Symmetric encryption means using the same key to encrypt and decrypt messages. Because of this feature, we also call this key "Shared Secret Key". The schematic diagram is as follows:

What is symmetric encryption?

Symmetric encryption algorithms (ciphers) widely used in modern cryptography include: AES (AES-128, AES-192, AES-256), ChaCha20, Twofish, IDEA, Serpent, Camelia, RC6, CAST, etc. Most of them are " Block Cipher " or " Block Cipher ", which can only encrypt fixed-size blocks (for example, 128 bits) at a time; a few are " Stream Cipher " )", the stream cipher algorithm encrypts data byte by byte into a ciphertext stream.

By using a technique called "block cipher working mode", a "block cipher algorithm" can be converted into a "stream cipher algorithm".

Quantum security

Even if computers enter the quantum era, current symmetric cryptography algorithms can still be used. Because most modern symmetric key cryptography algorithms are quantum -resistant , this means that powerful quantum computers cannot defeat their security when using keys of sufficient length. It currently appears that 256-bit AES/Twofish will be quantum safe for a long time .


2. Structure of symmetric encryption scheme

As we introduced, simply using data encryption algorithms can only ensure data security , but cannot meet our needs for message authenticity, integrity and non-repudiation . Therefore, we usually use symmetric encryption The algorithm is combined with other algorithms to form a " symmetric encryption scheme " for use. This "encryption scheme" composed of multiple cryptographic algorithms can simultaneously ensure the security, authenticity, integrity and non-repudiation of data.

block encryption scheme usually includes the following algorithms:

  • Key derivation algorithm KDF (such as Scrypt or Argon2) that converts passwords into keys: By using KDF, encryption schemes can allow users to use character passwords as "Shared Secret Keys" and make password cracking difficult and slow
  • Block cipher working mode (used to convert block ciphers to stream ciphers, such as CBC or CTR) + message padding algorithm (such as PKCS7): Block cipher algorithms (such as AES) require the help of these two algorithms to encrypt data of any size
  • Block cipher algorithms (such as AES): securely encrypt fixed-length blocks of data using a key
    • Most popular symmetric encryption algorithms are block ciphers
  • Message authentication algorithm (such as HMAC): used to verify the authenticity, integrity, and non-repudiation of messages

stream cipher encryption scheme itself can encrypt data of any length, so there is no need for "block cipher mode" and "message padding algorithm".

For example, AES-256-CTR-HMAC-SHA256 represents an encryption scheme that uses AES-256 and Counter grouping mode for encryption and HMAC-SHA256 for message authentication. Other popular symmetric encryption schemes include ChaCha20-Poly1305 and AES-128-GCM, among which ChaCha20-Poly130 is a stream cipher encryption scheme. We will introduce these two encryption schemes separately later.


3. Block cipher working mode

Previously, we briefly introduced the " block cipher working mode " that can convert the "block cipher algorithm" into a "stream cipher algorithm" to encrypt data of any length. Here we will mainly introduce the block cipher working mode in detail (hereinafter referred to as " block cipher algorithm"). mode " or " XXX mode ").

The encryption scheme has a specific "grouping mode" name in its name, such as:

  • AES-256-GCM - AES cipher with 256-bit encryption key and GCM grouping mode
  • AES-128-CTR - AES cipher with 128-bit encryption key and CTR grouping mode
  • Serpent-128-CBC - Serpent cipher with 128-bit encryption key and CBC grouping mode

The main idea behind the "block cipher working mode" is to divide the plaintext into multiple fixed-length groups, and then repeatedly apply the block cipher algorithm on these groups for encryption/decryption to achieve secure encryption/decryption of data of any length.

Some grouping modes (such as CBC) require that the input is split into groups and a padding algorithm (such as adding special padding characters) is used to pad the last grouping to the block size. There are also grouping modes (such as CTR, CFB, OFB, CCM, EAX, and GCM) that require no padding at all because they perform an XOR operation directly between the plaintext portion and the internal cryptographic state at each step.

The process of using "packet mode" to encrypt large amounts of data is basically as follows:

  • Initialize encryption algorithm state (using encryption key + initialization vector IV)
  • The first block of encrypted data
  • Convert the current state of the encryption algorithm using encryption keys and other parameters
  • Encrypt next packet
  • Convert encryption state again
  • Encrypt the next group
  • And so on until all input data has been processed

The decryption process is completely similar to encryption: initialize the algorithm first, and then decrypt all packets in sequence, which may involve a conversion of the encryption state.

Let's introduce the two common grouping modes of CTR and GCM in detail.


0. Initial vector IV

Before introducing the specific grouping mode, you need to first understand the concept of initialization vector IV (Initialization Vector), which is sometimes called Salt or Nonce. The initial vector IV is usually a random number. Its main function is to add randomness to the ciphertext, so that the same plaintext will generate different ciphertexts if encrypted multiple times, thus ensuring the unpredictability of the ciphertext.

The size of the IV should be the same as the cipher block size. For example, AES, Serpent, and Camellia all only support 128-bit cipher blocks, so the IVs they require must also be 128 bits.

The IV usually does not need to be kept secret, but should be sufficiently random (unpredictable) and not allow reuse. A random and unpredictable IV should be used for each encrypted message.

A common mistake is to encrypt multiple messages with the same symmetric key and the same IV , which makes a variety of cryptographic attacks possible against most grouping modes.


Source: ryan4yin, https://thiscute.world/posts/practical-cryptography-basics-6-symmetric-key-ciphers/
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.