Implementation of Encryption and Decryption Algorithms in Python (AES/DES)

Implementation of Encryption and Decryption Algorithms in Python (AES/DES)

In modern information technology, the security of data has become increasingly important. Encryption algorithms are one of the key means to protect data privacy. In this article, we will introduce two commonly used symmetric encryption algorithms: AES (Advanced Encryption Standard) and DES (Data Encryption Standard). We will implement these algorithms using the <span>pycryptodome</span> library in Python.

1. Install Dependencies

First, you need to install the <span>pycryptodome</span> library. You can install it using the following command:

pip install pycryptodome

2. AES Encryption and Decryption

2.1 Overview of AES

AES is a symmetric encryption algorithm, which means the same key is used for both encryption and decryption. It supports key lengths of 128, 192, and 256 bits, but in this example, we will use a 128-bit key.

2.2 AES Implementation Code

Below is a simple example of AES encryption and decryption:

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os

def aes_encrypt(plain_text, key):
    cipher = AES.new(key, AES.MODE_CBC)
    ct_bytes = cipher.encrypt(pad(plain_text.encode(), AES.block_size))
    return cipher.iv + ct_bytes  # Return IV and ciphertext combination

def aes_decrypt(cipher_text, key):
    iv = cipher_text[:16]  # Extract IV
    ct = cipher_text[16:]  # Extract ciphertext
    cipher = AES.new(key, AES.MODE_CBC, iv)
    pt = unpad(cipher.decrypt(ct), AES.block_size)
    return pt.decode()

# Example usage:
key = os.urandom(16)  # Randomly generate a 128-bit key
plain_text = "Hello World!"
cipher_text = aes_encrypt(plain_text, key)
print("Cipher Text:", cipher_text.hex())
decrypted_text = aes_decrypt(cipher_text, key)
print("Decrypted Text:", decrypted_text)

2.3 Program Explanation

  • <span>os.urandom(16)</span> is used to generate a random 128-bit key.
  • <span>AES.new(key, AES.MODE_CBC)</span> creates a new AES object using CBC mode.
  • <span>pad()</span> and <span>unpad()</span> functions are used to handle the plaintext to ensure its length meets the block size requirement.
  • In the returned result, we combine the initialization vector (IV) with the ciphertext for use in subsequent decoding.

3. DES Encryption and Decryption

3.1 Overview of DES

DES is an earlier symmetric encryption standard, and its security is insufficient for modern requirements, so it is not recommended for new projects. However, for educational purposes, we still provide an implementation of DES.

3.2 DES Implementation Code

Below is a simple example of DES encryption and decryption:

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

def des_encrypt(plain_text, key):
    des_cipher = DES.new(key.ljust(8).encode(), DES.MODE_ECB)
    ct_bytes = des_cipher.encrypt(pad(plain_text.encode(), DES.block_size))
    return ct_bytes

def des_decrypt(ciphertext, key):
    des_cipher = DES.new(key.ljust(8).encode(), DES.MODE_ECB)
    pt_bytes = unpad(des_cipher.decrypt(ciphertext), DES.block_size)
    return pt_bytes.decode()

# Example usage:
key_des = "secret_k"   # Must be an 8-byte long string
plain_des_txt= "Hello"
cipher_des_txt=des_encrypt(plain_des_txt,key_des)
print("Cipher Text (DES):", cipher_des_txt.hex())
decrypted_des_txt=des_decrypt(cipher_des_txt,key_des)
print("Decrypted Text (DES):", decrypted_des_txt)

3.3 Program Explanation

  • In this example, the key must be 8 characters long, as this is the required length for DES.
  • ECB mode is used for operation, but note that in practical applications, ECB mode should be avoided due to its lack of security.
  • Similarly, the <span>pad()</span> and <span>unpad()</span> functions ensure that the plaintext meets the block size requirement.

Conclusion

This article introduced how to implement AES and DES symmetric encryption algorithms in Python. Although both can effectively protect data, due to security considerations, it is recommended to prioritize the more robust AES in practical applications and to avoid using outdated or insecure methods like DES. Additionally, it is essential to manage your keys properly to ensure security during data transmission.

Leave a Comment