๐Ÿ” Practical 2: DES Encryption and Decryption

Complete Interactive Guide to Data Encryption Standard

๐ŸŽฏObjective

To understand and implement the Data Encryption Standard (DES) using Python and the pycryptodome library for encryption and decryption of data.

๐ŸŽฏPurpose of the Practical

To implement DES (Data Encryption Standard), a symmetric-key block cipher algorithm. The goal is to learn how modern encryption algorithms work using fixed-size keys and blocks, and how padding and modes affect secure communication.

๐Ÿ“šTheoretical Background

What is DES?

DES (Data Encryption Standard) is a symmetric-key block cipher developed in the 1970s by IBM and adopted by NIST in 1977. It encrypts data in 64-bit blocks using a 56-bit key (the actual key size is 64 bits, but 8 bits are used for parity). It operates in multiple rounds (16) with permutation and substitution steps.

It's now considered insecure for many applications due to its small key size and vulnerability to brute-force attacks, but it remains important for learning.

Block Size
64 bits
Key Size
56 bits (64 incl. parity)
Type
Symmetric Cipher
Structure
Feistel Network
Rounds
16

Modes of Operation

  • ECB (Electronic Codebook) โ€“ simplest mode, encrypts each block independently (used here)
  • CBC (Cipher Block Chaining) โ€“ more secure in practice
  • CFB (Cipher Feedback) โ€“ stream cipher mode
  • OFB (Output Feedback) โ€“ another stream cipher mode
  • CTR (Counter) โ€“ counter mode for parallel processing

๐Ÿ”งInstalling pycryptodome Library

In Google Colab, use the following command:

!pip install pycryptodome

This command installs the pycryptodome library directly from the Python Package Index (PyPI) so that you can use cryptographic functions (like DES, AES, RSA, hashing, etc.) in your notebook.

Why It Works in Google Colab

In Colab notebooks, the ! is used to run shell/terminal commands (just like in Jupyter Notebook). Google Colab runs on a virtual machine where packages may not be pre-installed. So, use !pip install pycryptodome to install it temporarily in that Colab session.

What Is pycryptodome?

It is a Python library that provides:

Visit: https://pycryptodome.readthedocs.io/ for more information
Important Notes:
  • You need to run !pip install pycryptodome every time you open a new Colab session unless you save the environment
  • This installation works only within the current notebook session
  • ๐Ÿ’ป Complete Python Code Implementation

    from Crypto.Cipher import DES
    from Crypto.Util.Padding import pad, unpad

    # 8-byte key (64 bits)
    key = b'12345678'

    # 8-byte block size
    plaintext = b'GECBHARUCHBHOLAVGUJARATINDIA'

    # Create DES cipher
    cipher = DES.new(key, DES.MODE_ECB)

    # Encrypt the plaintext
    ciphertext = cipher.encrypt(pad(plaintext, 8))
    print("Encrypted (hex):", ciphertext.hex())

    # Decrypt the ciphertext
    decrypted = unpad(cipher.decrypt(ciphertext), 8)
    print("Decrypted text:", decrypted.decode())

    Expected Output:

    Encrypted (hex): ad3806601cfc2d1efeb959b7d4642fcb
    Decrypted text: GECBHARUCHBHOLAVGUJARATINDIA

    ๐Ÿš€ Interactive DES Demo

    Click "Encrypt" to see the encrypted result...

    โš ๏ธLimitations of DES

    ๐Ÿ”ฌRelated Theories

    Symmetric Key Encryption: Same key used for encryption and decryption
    Block Cipher: Encrypts data in fixed-size blocks
    Feistel Structure: Design model used in DES
    Modes of Operation: ECB, CBC, etc., define how blocks are processed

    ๐ŸŒReal-Life Applications

    While DES is outdated and replaced by AES, it still has educational and historical significance:

    • Legacy systems still use DES (e.g., old banking systems)
    • Smart card encryption and low-resource devices in early cryptographic applications
    • Foundation for understanding more secure algorithms like AES and Triple DES

    ๐Ÿ“‹Important Notes

    ๐Ÿ”Security Aspects

    Much stronger than Caesar Cipher, but still not considered secure today due to:

    Security Vulnerabilities:
  • Small key size (56 bits) โ€” vulnerable to brute-force attacks
  • ECB mode leaks patterns โ€” not semantically secure
  • Replaced by AES and Triple DES in modern standards
  • ๐Ÿ”Key Observations

    DES encrypts data in blocks, unlike Caesar which is character-based
    Requires exact block sizes or uses padding (like pad() and unpad())
    Both encryption and decryption use the same key, i.e., it's a symmetric cipher
    The use of libraries like pycryptodome abstracts the complex mathematical operations

    ๐Ÿ“ŠComparison with Practical 1 (Caesar Cipher)

    Feature Practical 1: Caesar Cipher Practical 2: DES
    Type Substitution Cipher Block Cipher
    Key Length 1 integer (shift) 64-bit binary key
    Security Level Very Low Medium (historically), Weak (now)
    Encryption Granularity Character-level 64-bit Block-level
    Mode Manual shift ECB Mode (can vary)
    Real Use Educational only Previously used in banking & systems
    Vulnerability Frequency analysis, brute force Brute force, pattern leakage in ECB
    Library None pycryptodome (DES module)

    ๐Ÿ“ Summary

    This practical demonstrates students to modern symmetric key encryption using DES. It demonstrates how encryption operates on binary blocks, requires specific key formats, and includes padding and modes of operation. While DES is outdated, it provides a stepping stone to understanding secure and practical encryption mechanisms like AES, showcasing how much more advanced modern cryptography is compared to simple substitution techniques like Caesar Cipher.

    ๐ŸŽ“Learning Outcomes

    After completing this practical, students will: