Triple DES (3DES) Encryption

ECB and CBC Modes Implementation

Practical 4: Cryptography & Network Security

🎯 Objective

To implement Triple DES (3DES) encryption and decryption in ECB and CBC modes using Python and understand the working and security differences between these modes.

📚 Theoretical Background

What is Triple DES (3DES)?

3DES is an enhanced version of the DES (Data Encryption Standard) that applies the DES algorithm three times using either two or three different keys.

DES

56 bits

2-Key 3DES

112 bits

3-Key 3DES

168 bits

Modes of Operation

ECB (Electronic Codebook)

Each block is encrypted independently. Weak due to pattern leaks.

CBC (Cipher Block Chaining)

Each block is XORed with the previous ciphertext block. Requires IV, more secure.

💻 Implementation Code

from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes

# 3DES requires a 24-byte key (168 bits)
key = DES3.adjust_key_parity(get_random_bytes(24)) # Adjust for proper parity
plaintext = b'GEC BHARUCH COLLEGE' # Must be padded for block size (8 bytes)
iv = get_random_bytes(8) # IV for CBC mode

# --- ECB MODE ---
cipher_ecb = DES3.new(key, DES3.MODE_ECB)
ciphertext_ecb = cipher_ecb.encrypt(pad(plaintext, 8))
decrypted_ecb = unpad(cipher_ecb.decrypt(ciphertext_ecb), 8)

# --- CBC MODE ---
cipher_cbc = DES3.new(key, DES3.MODE_CBC, iv)
ciphertext_cbc = cipher_cbc.encrypt(pad(plaintext, 8))
decrypted_cbc = unpad(DES3.new(key, DES3.MODE_CBC, iv).decrypt(ciphertext_cbc), 8)

# --- Output ---
print("3DES ECB Encrypted (hex):", ciphertext_ecb.hex())
print("3DES ECB Decrypted:", decrypted_ecb.decode())

print("\n3DES CBC Encrypted (hex):", ciphertext_cbc.hex())
print("3DES CBC Decrypted:", decrypted_cbc.decode())

📤 Output

3DES ECB Encrypted (hex): e2607e601d5d9bba7b7ae4497bd7172ca2fb789e37a5a938
3DES ECB Decrypted: GEC BHARUCH COLLEGE

3DES CBC Encrypted (hex): de07ac58b6beea56fe66293701bb5dbe5995ed1c7c73797a
3DES CBC Decrypted: GEC BHARUCH COLLEGE

🔍 IV Use

ECB Mode

Does not use an IV - each block encrypted independently

CBC Mode

Requires an Initialization Vector (IV) for secure chaining

⚖️ ECB vs CBC Comparison

Feature ECB CBC
IV Required ❌ No ✅ Yes
Pattern Leaks ❌ Yes (less secure) ✅ No (more secure)
Parallel Encryption ✅ Yes ❌ No
Error Propagation ❌ Affects only 1 block ✅ Affects current + next block

🏢 Real-Life Applications

Triple DES was designed as an interim solution to strengthen DES without needing an entirely new algorithm:

  • ATM PIN encryption and smart cards
  • EMV chip cards for payment processing
  • Financial systems (SWIFT, early versions of TLS)
  • Visa/Mastercard transaction processing
  • Legacy systems requiring backward compatibility

🔒 Security Aspects

Key Points:

  • 3DES performs DES encryption three times (EDE structure)
  • Effective security is ~112 bits due to meet-in-the-middle attacks
  • CBC mode provides semantic security with random IV
  • ECB mode is insecure and should only be used for demonstration
  • 3DES is now considered deprecated for new systems (NIST recommends AES)

📊 Algorithm Comparison

Feature DES Triple DES (3DES) AES
Key Size 56 bits 168 bits (effective 112 bits) 128/192/256 bits
Block Size 64 bits 64 bits 128 bits
Cipher Type Symmetric Block Cipher Symmetric Block Cipher Symmetric Block Cipher
Strength Weak Moderate (deprecated by NIST) Strong
Speed Fast (legacy) Slow (3× DES operations) Very Fast
Applications Historical/legacy Financial systems, legacy use Modern (TLS, AES-256)

🔑 Key Observations

Triple Processing

Encrypts with DES → decrypts with DES → encrypts with DES (EDE structure)

CBC Security

Same plaintext results in different ciphertext when using CBC due to random IV

Performance

3DES is significantly slower than AES due to triple processing

Evolution

Although stronger than DES, 3DES is less efficient and less secure than AES

🎓 Learning Outcomes

After this practical, students will be able to:

  • Understand Triple DES (3DES) and its key expansion logic
  • Implement encryption and decryption in ECB and CBC modes
  • Learn the importance of IV in block cipher chaining (CBC)
  • Understand the weakness of ECB and advantages of CBC
  • Use Python and pycryptodome for cryptographic programming

📝 Summary

This practical demonstrates Triple DES (3DES) as an enhancement over DES by applying it three times and introduces block cipher modes like ECB and CBC. It highlights the evolution of symmetric encryption: DES → 3DES → AES.

While 3DES offered better security than DES, it is now obsolete due to performance and cryptographic limitations. However, learning 3DES helps understand cipher design evolution and the importance of secure modes of operation like CBC.