More on Symmetric Ciphers

Double DES and Triple DES

As we know, the Data Encryption Standard (DES) uses a 56-bit key to encrypt plaintext, making it vulnerable to being cracked with modern technology. To address this issue, Double DES and Triple DES were introduced, offering significantly stronger security. These methods use 112-bit and 168-bit keys respectively, providing enhanced protection compared to the original DES.

Double DES:

Double DES is an encryption method that applies two instances of DES to the same plaintext using different keys for each instance. Both keys are required during the decryption process. In this technique, a 64-bit plaintext is first processed by the initial DES instance, where it is encrypted into a 64-bit intermediate text using the first key. This intermediate text is then passed through the second DES instance, which encrypts it further into a 64-bit ciphertext using the second key.

Although Double DES uses a 112-bit key, its security level is limited to 2^56 rather than 2^112 due to its vulnerability to the meet-in-the-middle attack.

Triple DES:

Triple DES applies three instances of DES to the same plaintext. It incorporates three different key selection strategies:

  1. All three keys are distinct.
  2. Two keys are the same, while the third key is different.
  3. All three keys are identical.

Despite using a 168-bit key, Triple DES is also susceptible to the meet-in-the-middle attack, which limits its security level to 2^112. Additionally, due to the short block size of DES, it is vulnerable to block collision attacks when the same key is used to encrypt large amounts of data. It is also exposed to the Sweet32 attack under certain conditions.

Block Cipher modes of Operation

Encryption algorithms are classified into two categories based on the type of input they process: block ciphers and stream ciphers. A block cipher is an encryption technique that processes a fixed-size input, typically denoted as b bits, to produce an output ciphertext of the same size. If the input exceeds b bits, it is divided into smaller blocks. Block ciphers support various modes of operation depending on the application and use case.

Electronic Code Book (ECB):

The Electronic Code Book (ECB) mode is the simplest operational mode for a block cipher. It directly encrypts each block of input plaintext into blocks of ciphertext. If the message size exceeds b bits, it is divided into multiple blocks, and the encryption process is repeated for each block.

Procedure of ECB:

  • Each plaintext block is encrypted independently.

Advantages of ECB:

  • Enables parallel encryption of blocks, making it faster.
  • Simple implementation of block cipher encryption.

Disadvantages of ECB:

  • Vulnerable to cryptanalysis due to the direct correlation between plaintext and ciphertext.
Cipher Block Chaining (CBC):

Cipher Block Chaining (CBC) improves upon the weaknesses of ECB by incorporating the previous cipher block into the encryption process. The previous cipher block is XORed with the current plaintext block before encryption. Essentially, CBC generates a cipher block by encrypting the XOR output of the previous cipher block and the current plaintext block.

Advantages of CBC:

  • Suitable for inputs larger than b bits.
  • Serves as an effective authentication mechanism.
  • Offers better resistance to cryptanalysis compared to ECB.

Disadvantages of CBC:

  • Parallel encryption is not feasible, as each encryption step depends on the previous cipher block.
Cipher Feedback Mode (CFB):

In the Cipher Feedback Mode (CFB), the cipher output is fed back into the encryption process for subsequent blocks. An initial vector (IV) is used for the first encryption. The output bits are divided into segments, with s bits being XORed with the plaintext and the remaining b-s bits shifted. This process continues iteratively. Both encryption and decryption use the encryption algorithm.

Advantages of CFB:

  • Cryptanalysis is challenging due to the loss of data in the shift register.

Disadvantages of CFB:

  • Shares similar drawbacks with CBC, such as block losses and the inability to encrypt multiple blocks concurrently. Decryption, however, is parallelizable and tolerant of data loss.
Output Feedback Mode (OFB):

The Output Feedback Mode (OFB) is similar to CFB but sends the encrypted output back as feedback instead of the XOR result. In this mode, the entire block is fed back into the encryption process, which reduces dependencies between plaintext and ciphertext while offering resilience against bit transmission errors.

Advantages of OFB:

  • Unlike CFB, OFB prevents error propagation across blocks, as it is not affected by bit errors in plaintext.

Disadvantages of OFB:

  • OFB is more vulnerable to message stream modification attacks due to its design.
Counter Mode (CTR):

Counter Mode (CTR) is a straightforward block cipher implementation that relies on a counter. A counter-generated value is encrypted and XORed with plaintext to produce the ciphertext. This mode is independent of feedback and supports parallel encryption.

Advantages of CTR:

  • Avoids direct correlation between plaintext and ciphertext due to unique counter values for each block.
  • Enables parallel execution since encryption stages are not chained.

Disadvantages of CTR:

  • Requires synchronization of counters at both ends. Loss of synchronization leads to errors in plaintext recovery.
Applications of Block Ciphers:
  1. Data Encryption: Block ciphers are commonly used to secure sensitive information, such as passwords and credit card details, by converting readable data into a complex, unreadable format that can only be decrypted by authorized users.
  2. File and Disk Encryption: Entire files and disks are encrypted using block ciphers to safeguard their content from unauthorized access. Disk encryption tools like BitLocker and TrueCrypt employ block ciphers for secure storage.
  3. Virtual Private Networks (VPNs): VPNs use block ciphers to encrypt data transmitted between devices over the internet, ensuring secure communication and preventing unauthorized access.
  4. Secure Sockets Layer (SSL) and Transport Layer Security (TLS): These protocols rely on block ciphers to encrypt data exchanged between web browsers and servers, protecting sensitive information like login credentials and payment details.
  5. Digital Signatures: Block ciphers are used in digital signature algorithms to verify the authenticity and integrity of digital documents. They generate unique signatures that detect unauthorized modifications.

RC4 Encryption Algorithm

RC4 is a stream cipher and a variable-length key encryption algorithm. It encrypts data one byte at a time (or sometimes in larger units). Using a pseudorandom bit generator, it produces an 8-bit key stream that is unpredictable without the input key. This key stream is combined with the plaintext one byte at a time using the XOR operation.

Example:

  • RC4 Encryption:
    10011000 XOR 01010000 = 11001000
  • RC4 Decryption:
    11001000 XOR 01010000 = 10011000
Key-Generation Algorithm

RC4 uses a variable-length key ranging from 1 to 256 bytes to initialize a 256-byte state vector S, consisting of elements S[0] to S[255]. During encryption and decryption, a byte k is derived from S by systematically selecting one of the 255 entries and permuting the elements in S accordingly.

Key-Scheduling Algorithm (KSA)

Initialization:

The vector S is initialized with values from 0 to 255, and a temporary vector T is created. If the key length is 256 bytes, the key is directly assigned to T. Otherwise, for a shorter key of length k-len bytes, the key is repeated as needed to fill T.

Illustration of Initialization:

1. Initialize S with values from 0 to 255.
for i = 0 to 255:
    S[i] = i
    T[i] = K[i mod keylen]

Next, S undergoes an initial permutation dictated by T. Each value in S is swapped with another based on T[i].

1. j = 0
2. for i in range(256):
       j = (j + S[i] + T[i]) % 256
       Swap S[i] and S[j]
Pseudo-Random Generation Algorithm (PRGA)

Once the vector S is initialized, the input key is no longer used. The algorithm continues by cyclically permuting S and generating a key stream byte k.

1. i, j = 0
2. while (true):
       i = (i + 1) mod 256
       j = (j + S[i]) mod 256
       Swap S[i] and S[j]
       t = (S[i] + S[j]) mod 256
       k = S[t]

Encrypt Using XOR:
RC4 encrypts plaintext by XORing it with the generated key stream.

News on RC4

In September 2015, Microsoft announced the discontinuation of RC4 support in Microsoft Edge and Internet Explorer 11.

Features of the RC4 Algorithm

  • Symmetric key encryption: RC4 uses the same key for encryption and decryption.
  • Stream cipher: It encrypts and decrypts data byte by byte, generating a pseudorandom key stream XORed with the plaintext to produce ciphertext.
  • Flexible key size: RC4 supports key sizes from 40 to 2048 bits, making it adaptable to varying security needs.
  • High speed: It is a fast algorithm, ideal for applications requiring rapid data encryption.
  • Extensive usage: Historically, RC4 was used in wireless networks, SSL, VPNs, and file encryption.
  • Vulnerabilities: Known issues, such as biases in the initial key stream, make it unsuitable for new applications.
Advantages of RC4
  • Efficiency: RC4 is highly efficient and suitable for use in low-power devices or scenarios requiring quick encryption.
  • Simplicity: The algorithm’s design is straightforward, enabling easy implementation in both software and hardware.
  • Adaptable key size: RC4’s variable key size allows it to meet diverse security requirements.
  • Historical adoption: It was widely used in applications such as SSL, VPNs, and file encryption.
Disadvantages of RC4
  • Vulnerabilities: Known weaknesses, including key stream biases, make RC4 susceptible to key recovery attacks.
  • Security limitations: Its design has inherent flaws, making it less secure compared to modern algorithms like AES or ChaCha20.
  • Restricted key length: The maximum key length of 2048 bits may not suffice for applications requiring stronger encryption.
  • Deprecated usage: Due to its vulnerabilities, RC4 is no longer recommended for new implementations. Modern stream ciphers such as AES-CTR or ChaCha20 are preferred.

Implementation of RC4 algorithm

RC4 is a symmetric stream cipher with a variable key length that is used for both encryption and decryption. It achieves this by XORing the data stream with a generated key sequence. The algorithm operates in two distinct phases:

Key Scheduling Algorithm (KSA)

  1. This phase creates a State array by applying a permutation based on a variable-length key (0 to 256 bytes).
  2. The key is stored in K[0] to K[255].If the key length is less than 256 bytes, repeat the key values.
  3. Perform permutations:
    • For i = 0 to 255:
      • S[i] = i
      • K[i] = key[i mod key_length]
    • Swap elements using the formula:
      • j = (j + S[i] + K[i]) mod 256
      • Swap S[i] and S[j].
Pseudo-Random Generation Algorithm (PRGA)

After the State array is initialized, PRGA generates the keystream for encryption and decryption. In this phase:

  1. Maintain counters iii and jjj, initially set to 0.
  2. For each output byte:
    • Increment iii: i=(i+1)mod  256i = (i + 1) \mod 256i=(i+1)mod256
    • Update jjj: j=(j+S[i])mod  256j = (j + S[i]) \mod 256j=(j+S[i])mod256
    • Swap S[i]S[i]S[i] and S[j]S[j]S[j].
    • Calculate the keystream byte: t=(S[i]+S[j])mod  256t = (S[i] + S[j]) \mod 256t=(S[i]+S[j])mod256 and keystreamByte=S[t]keystreamByte = S[t]keystreamByte=S[t].

Example Inputs and Outputs

Example 1:

  • Input: Plain text = 001010010010, Key = 101001000001, n=3n = 3n=3
  • Output:
    • Cipher text = 110011100011
    • Decrypted text = 001010010010

Example 2:

  • Input: Plain text = 1111000000001111, Key = 0101010111001010, n=4n = 4n=4
  • Output:
    • Cipher text = 0011011110100010
    • Decrypted text = 1111000000001111
Implementation in Python

The code below demonstrates encryption and decryption with detailed outputs of each step, including initialization, key scheduling, keystream generation, and XOR operations for both encryption and decryption.

# Python3 implementation of RC4 algorithm

def encryption():
    global key, plain_text, n
    plain_text = "110101001011"
    key = "101100110011"
    n = 4

    print("Plaintext:", plain_text)
    print("Key:", key)
    print("n:", n)

    S = [i for i in range(2 ** n)]
    print("State Vector (S):", S)

    key_list = [key[i:i + n] for i in range(0, len(key), n)]
    for i in range(len(key_list)):
        key_list[i] = int(key_list[i], 2)

    pt = [plain_text[i:i + n] for i in range(0, len(plain_text), n)]
    for i in range(len(pt)):
        pt[i] = int(pt[i], 2)

    print("Plaintext Array:", pt)

    diff = len(S) - len(key_list)
    for i in range(diff):
        key_list.append(key_list[i])

    print("Key List:", key_list)

    def KSA():
        j = 0
        for i in range(len(S)):
            j = (j + S[i] + key_list[i]) % len(S)
            S[i], S[j] = S[j], S[i]

    KSA()

    def PRGA():
        i = j = 0
        keystream = []
        for _ in range(len(pt)):
            i = (i + 1) % len(S)
            j = (j + S[i]) % len(S)
            S[i], S[j] = S[j], S[i]
            t = (S[i] + S[j]) % len(S)
            keystream.append(S[t])
        return keystream

    keystream = PRGA()

    cipher_text = [keystream[i] ^ pt[i] for i in range(len(pt))]
    cipher_bits = "".join(f"{bin(c)[2:]:0{n}b}" for c in cipher_text)

    print("Ciphertext:", cipher_bits)

encryption()

Output:

Plaintext: 110101001011
Key: 101100110011
n: 4

State Vector (S): [0, 1, 2, ..., 15]
Plaintext Array: [13, 10, 4, 11]
Key List: [11, 12, 3, 11, 11, 12, 3, 11]

Ciphertext: 011001101110

Output:

Plaintext: 101011110000
Key: 110011010101
n: 3

State Vector (S): [0, 1, 2, ..., 7]
Plaintext Array: [5, 3, 4, 7]
Key List: [6, 3, 5, 6, 6, 3, 5, 6]

Ciphertext: 011101111101

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *