sameer fakhoury
  • Home
  • CTF Writeups
  • Course Summaries
  • Cyber Reports
  • Articles
  • Event Notes
  • About Me
Enhancing Cybersecurity with PyCryptoDome in Python

Enhancing Cybersecurity with PyCryptoDome in Python

Category
Programming Languages
Level
Intermediate
Number
28

Key terms to be familiar with before delving into this blog post.

  1. Plain Text: Original human-readable data before encryption.
  2. Cipher Text: Encrypted, unreadable data resulting from encryption.
  3. Symmetric Encryption: Uses the same key for encryption and decryption.
  4. Asymmetric Encryption: Uses a pair of public and private keys for secure communication.
    1. Public Key: Shared openly in asymmetric encryption for encryption or verification.
    2. Private Key: Secret counterpart in asymmetric encryption used for decryption or signing.
  5. AES (Advanced Encryption Standard): Widely-used symmetric encryption for robust data protection.

In the dynamic realm of cybersecurity, having robust encryption tools is paramount to safeguard sensitive information.

One such versatile and user-friendly library for cryptographic operations in Python is PyCryptoDome, PyCryptoDome provides a seamless way to integrate cryptographic functionalities into your Python applications.

PyCryptoDome simplifies cryptographic operations, making them accessible to a broader audience. With just a few lines of code, you can implement strong encryption and decryption mechanisms.

Let's take a simple example of encrypting a message using the Advanced Encryption Standard (AES) algorithm, a widely used symmetric encryption algorithm

  1. Import necessary modules: Crypto.Cipher for AES and Crypto.Random for random bytes.
  2. Generate a random 128-bit key using get_random_bytes.
  3. Set a sample message.
  4. Call encrypt_decrypt_message with the key and message, obtaining the encrypted and decrypted messages.
  5. Define encrypt_decrypt_message function: a. Create AES cipher in EAX mode with a key. b. Generate nonce → number used once c. Encrypt UTF-8 encoded message and get ciphertext. d. Create a new cipher for decryption with the same key and nonce. e. Decrypt ciphertext and decode to obtain the original message. f. Return ciphertext and decrypted message.
  6. Print the original message, encrypted message, and decrypted message for verification.

Summary:

  1. PyCryptoDome offers a user-friendly interface for cryptographic operations in Python.
  2. Implementation of strong encryption and decryption is simplified, making it accessible to both intermediate and advanced users.
  3. The provided code snippet showcases how to use PyCryptoDome for AES encryption, ensuring secure communication in Python applications.

©sameer fakhoury

GitHubLinkedIn
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

def encrypt_decrypt_message(key, message):
		# Encryption
    cipher = AES.new(key, AES.MODE_EAX)
    nonce = cipher.nonce
    ciphertext = cipher.encrypt(message.encode('utf-8'))

    # Decryption
    decipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
    decrypted_message = decipher.decrypt(ciphertext).decode('utf-8')
    
    return ciphertext, decrypted_message

# Example usage:
key = get_random_bytes(16)  # 128-bit key
message = "Hello, PyCryptoDome!"

encrypted_message, decrypted_message = encrypt_decrypt_message(key, message)

print(f"Original Message: {message}")
print(f"Encrypted Message: {encrypted_message}")
print(f"Decrypted Message: {decrypted_message}")