Introduction

Setting Up Your Environment

Before we dive into cryptographic techniques, we need to set up the right environment for writing and executing Python code. We'll be using Google Colab and Jupyter Notebooks throughout this tutorial, as they provide an easy-to-use, interactive coding environment.

Option 1: Google Colab

Google Colab is a cloud-based platform that allows you to run Python code in a browser without the need for any local installation. It's perfect for quickly testing cryptographic programs without worrying about dependencies.

To get started:

  1. Sign in with your Google account.

  2. Create a new notebook by clicking on "File > New Notebook".

Option 2: Jupyter Notebooks

If you prefer to run the code locally, Jupyter Notebooks is an excellent choice. To set it up:

  1. Install Anaconda (a Python distribution that includes Jupyter) from here.

  2. Open the Anaconda Navigator and launch Jupyter Notebook.

  3. Navigate to your working directory and create a new notebook.


Installing Necessary Libraries

We will be using several Python libraries to implement cryptographic functions. Two essential libraries for this tutorial are:

  • pycryptodome: A library that provides cryptographic functions (e.g., encryption algorithms, hashing).

  • hashlib: Part of the Python Standard Library, used for cryptographic hashing.

To install pycryptodome, run the following command in your Jupyter notebook or Colab cell:

!pip install pycryptodome

First Cryptographic Program: Simple Encryption/Decryption

Let’s start with a basic example of encryption and decryption using the AES (Advanced Encryption Standard) algorithm in CBC (Cipher Block Chaining) mode. This is one of the most widely used encryption standards today.

We’ll use the pycryptodome library to implement this.

Step 1: Import the Required Libraries

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

Step 2: Define the Data to Encrypt

We’ll encrypt a simple message, such as "Hello, World!".

# Data to be encrypted (must be in bytes)
data = b"Hello, World!"

Step 3: Generate a Random Key and Initialization Vector (IV)

AES encryption requires both a key and an IV (initialization vector). The key is secret, while the IV ensures that the same plaintext will produce different ciphertexts each time.

# Generate a random 16-byte key and IV (AES-128)
key = get_random_bytes(16)
iv = get_random_bytes(16)

Step 4: Encrypt the Data

Now, let’s encrypt the data using AES in CBC mode.

# Create AES cipher object in CBC mode
cipher = AES.new(key, AES.MODE_CBC, iv)

# Pad the data to be a multiple of the block size (16 bytes)
ciphertext = cipher.encrypt(pad(data, AES.block_size))

print("Ciphertext:", ciphertext)

Step 5: Decrypt the Data

To decrypt the data, we need to use the same key and IV that were used during encryption.

# Create a new AES cipher object for decryption
cipher_decrypt = AES.new(key, AES.MODE_CBC, iv)

# Decrypt and unpad the data
decrypted_data = unpad(cipher_decrypt.decrypt(ciphertext), AES.block_size)

print("Decrypted Data:", decrypted_data.decode('utf-8'))

Output:

• The encrypted ciphertext will be shown as a string of random-looking bytes.

• The decrypted data will match the original input: "Hello, World!".

Congratulations! You’ve just implemented your first cryptographic program, using AES encryption and decryption. This foundational example will help you understand the basics of symmetric encryption, which we’ll build upon in later sections.

Last updated