Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

Create Double DES (2DES).md

Abdul Wahab Junaid76d4dce950ee
diff --git a/Cryptography/Cryptography Course/Course/Double DES (2DES).md b/Cryptography/Cryptography Course/Course/Double DES (2DES).md
new file mode 100644
index 0000000..2d35760
--- /dev/null
+++ b/Cryptography/Cryptography Course/Course/Double DES (2DES).md
@@ -0,0 +1,114 @@
+### **Cryptography - Double DES (2DES)**
+
+**Double DES (2DES)** is an encryption method that applies the **Data Encryption Standard (DES)** encryption algorithm twice in succession with two different keys to increase security. Unlike **Triple DES (3DES)**, which applies DES three times, 2DES uses two separate 56-bit keys, aiming to extend DES's security without the computational overhead of a third round of encryption. However, due to vulnerabilities such as **meet-in-the-middle (MITM) attacks**, 2DES does not achieve the expected level of security increase and is rarely used in practice.
+
+---
+
+### **How Double DES (2DES) Works**
+
+In 2DES, encryption is done as follows:
+
+1. **Plaintext (P):** A 64-bit plaintext block.
+2. **First Encryption:** The plaintext is encrypted using the first key (K1) with DES, resulting in intermediate ciphertext (C1).
+ $\[
+ C1 = \text{DES}_{K1}(P)
+ \]$
+3. **Second Encryption:** The intermediate ciphertext (C1) is then encrypted again using a second key (K2) with DES, resulting in the final ciphertext (C2).
+ $\[
+ C2 = \text{DES}_{K2}(C1)
+ \]$
+4. **Final Ciphertext (C2):** The result (C2) is the 2DES-encrypted ciphertext.
+
+The process for **decryption** is simply the reverse:
+
+1. **First Decryption:** The ciphertext (C2) is decrypted with the second key (K2), yielding the intermediate ciphertext (C1).
+ $\[
+ C1 = \text{DES}_{K2}^{-1}(C2)
+ \]$
+2. **Second Decryption:** The intermediate ciphertext (C1) is decrypted with the first key (K1) to produce the original plaintext (P).
+ $\[
+ P = \text{DES}_{K1}^{-1}(C1)
+ \]$
+
+---
+
+### **Security of Double DES and the Meet-in-the-Middle Attack**
+
+When DES was found to be vulnerable due to its short 56-bit key length, it seemed that applying DES twice (2DES) with two keys would provide security roughly equivalent to having a 112-bit key. However, **meet-in-the-middle (MITM)** attacks demonstrate that this is not the case.
+
+**Meet-in-the-Middle Attack on 2DES:**
+- The meet-in-the-middle attack reduces the effective security of 2DES to approximately **57 bits**, which is not much stronger than single DES.
+- Here’s how the attack works:
+ 1. **Attack Setup:** The attacker knows pairs of plaintext and ciphertext. They want to recover the two 56-bit keys (K1 and K2).
+ 2. **Forward Search:** The attacker encrypts the plaintext with all possible values for K1, storing each result.
+ 3. **Backward Search:** The attacker decrypts the ciphertext with all possible values for K2 and checks for matches with the intermediate values from step 2.
+ 4. **Match Identification:** Once a match is found, it’s highly likely that the corresponding values of K1 and K2 are the encryption keys.
+
+- This MITM attack requires **2^56** operations to perform, effectively making 2DES only marginally more secure than DES itself, thus failing to achieve the desired 112-bit security level.
+
+---
+
+### **Why Double DES is Not Used in Practice**
+
+Due to the susceptibility of 2DES to meet-in-the-middle attacks, it does not provide adequate security, so it is not widely used. Instead, **Triple DES (3DES)** is typically used, as the third DES application offers better resistance to meet-in-the-middle attacks.
+
+---
+
+### **Example Implementation of Double DES (Python)**
+
+To demonstrate the encryption and decryption process of 2DES, here’s an example in Python:
+
+```python
+from Crypto.Cipher import DES
+from Crypto.Util.Padding import pad, unpad
+from Crypto.Random import get_random_bytes
+
+# Double DES encryption
+def double_des_encrypt(plaintext, key1, key2):
+ # First DES encryption with key1
+ cipher1 = DES.new(key1, DES.MODE_ECB)
+ ciphertext1 = cipher1.encrypt(pad(plaintext, DES.block_size))
+
+ # Second DES encryption with key2
+ cipher2 = DES.new(key2, DES.MODE_ECB)
+ final_ciphertext = cipher2.encrypt(ciphertext1)
+
+ return final_ciphertext
+
+# Double DES decryption
+def double_des_decrypt(ciphertext, key1, key2):
+ # First DES decryption with key2
+ cipher2 = DES.new(key2, DES.MODE_ECB)
+ decrypted_intermediate = cipher2.decrypt(ciphertext)
+
+ # Second DES decryption with key1
+ cipher1 = DES.new(key1, DES.MODE_ECB)
+ plaintext = unpad(cipher1.decrypt(decrypted_intermediate), DES.block_size)
+
+ return plaintext
+
+# Example usage
+key1 = get_random_bytes(8) # First 56-bit DES key
+key2 = get_random_bytes(8) # Second 56-bit DES key
+plaintext = b"Double DES Encryption Example!"
+
+# Encrypting with Double DES
+ciphertext = double_des_encrypt(plaintext, key1, key2)
+print("Encrypted:", ciphertext)
+
+# Decrypting with Double DES
+decrypted_text = double_des_decrypt(ciphertext, key1, key2)
+print("Decrypted:", decrypted_text.decode())
+```
+
+**Explanation of the Code:**
+- **Keys:** We generate two random 8-byte (56-bit) DES keys.
+- **Padding:** The plaintext is padded to be a multiple of the DES block size (64 bits).
+- **Double DES Encryption:** We first encrypt with **key1**, then take the result and encrypt it again with **key2**.
+- **Double DES Decryption:** Decryption is performed by reversing the process: decrypt with **key2**, then decrypt the intermediate result with **key1**.
+
+---
+
+### **Conclusion**
+
+Double DES is an example of how reapplying an encryption algorithm with multiple keys does not necessarily increase security by a simple factor. Due to its vulnerability to the meet-in-the-middle attack, 2DES is not widely adopted. Instead, **Triple DES (3DES)**, which applies DES three times, has become the preferred method for enhancing DES security in legacy systems. However, with the advent of stronger algorithms like **AES (Advanced Encryption Standard)**, both DES and its double and triple variants are being phased out for more secure alternatives.