Data Encryption Techniques in Python: Common Encryption Algorithms and Their Implementation

Data Encryption Techniques in Python: Common Encryption Algorithms and Their Implementation

In today’s information age, data security is particularly important. Whether it is personal privacy or corporate secrets, protecting data from unauthorized access is crucial. This article will introduce several commonly used data encryption algorithms and demonstrate how to implement these algorithms using Python code examples.

Basics of Encryption

What is Encryption?

Encryption is a process that converts plain information (plaintext) into an unreadable form (ciphertext) to prevent unauthorized access. Only those with a specific decoding key can revert it back to plaintext.

Symmetric and Asymmetric Encryption

  • Symmetric Encryption: Uses the same key for both encryption and decryption of data. Examples include AES and DES.
  • Asymmetric Encryption: Uses a pair of public and private keys for data encryption and decryption. An example is RSA.

Common Encryption Algorithms

1. AES (Advanced Encryption Standard)

AES is a widely used symmetric encryption standard that supports key lengths of 128, 192, and 256 bits.

Installing the Dependency Library

First, we need to install the <span>pycryptodome</span> library, which is used for various cryptographic operations. Run the following command in the command line:

pip install pycryptodome

AES Example Code

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

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)
    plain_text = unpad(cipher.decrypt(ct), AES.block_size).decode()
    return plain_text

# Example
key = os.urandom(16)  # Randomly generate a 16-byte long key
plain_text = "Hello World!"
cipher_text = aes_encrypt(plain_text, key)
print("Cipher Text:", cipher_text)
decrypted_message = aes_decrypt(cipher_text, key)
print("Decrypted Message:", decrypted_message)

2. RSA (Rivest-Shamir-Adleman)

RSA is an asymmetric encryption system commonly used for secure data transmission.

Installing the Dependency Library

We also need to install the <span>pycryptodome</span> library to implement RSA encryption and decryption:

pip install pycryptodome

RSA Example Code

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

def rsa_encrypt(public_key, plain_text):
    rsa_cipher = PKCS1_OAEP.new(public_key)
    encrypted_data = rsa_cipher.encrypt(plain_text.encode())
    return encrypted_data

def rsa_decrypt(private_key, encrypted_data):
    rsa_cipher = PKCS1_OAEP.new(private_key)
    decrypted_data = rsa_cipher.decrypt(encrypted_data).decode()
    return decrypted_data

# Example: Generate public and private keys and perform RSA encryption and decryption
key_pair = RSA.generate(2048)
private_key = key_pair.export_key()
public_key = key_pair.publickey().export_key()

message_to_encrypt = "Hello World!"
encrypted_message = rsa_encrypt(RSA.importKey(public_key), message_to_encrypt)
print("Encrypted Message:", encrypted_message)
decrypted_message = rsa_decrypt(RSA.importKey(private_key), encrypted_message)
print("Decrypted Message:", decrypted_message)

Conclusion

In this article, we introduced two common data encryption techniques: AES and RSA, and provided corresponding Python implementation examples. These techniques can help developers effectively protect sensitive data in applications. In practical applications, it is important to choose the appropriate algorithm and mode based on requirements. Additionally, ensure proper management of your keys to maintain overall system security. We hope this article helps you better understand and apply data encryption techniques.

Leave a Comment