Horje
What is pycryptodome in Python?

PyCrypto can be installed via pip, though it’s worth noting that it is considered outdated and has been superseded by the pycryptodome library, which is a more actively maintained fork.

What is pycryptodome?

PyCryptodome is a Python library that provides cryptographic functions and algorithms. It is a self-contained Python package offering a wide range of cryptographic operations, including encryption, decryption, hashing, and signature verification. PyCryptodome is a drop-in replacement for the old pycryptodome library and is designed to address many of its shortcomings, including security vulnerabilities and maintenance issues.

Key Features

  1. Symmetric Encryption: Provides algorithms for symmetric encryption, such as AES (Advanced Encryption Standard) and DES (Data Encryption Standard), allowing for secure data encryption and decryption using the same key.
  2. Asymmetric Encryption: Supports asymmetric encryption algorithms, such as RSA (Rivest–Shamir–Adleman), which use a pair of keys (public and private) for secure communication.
  3. Hash Functions: Includes cryptographic hash functions like SHA (Secure Hash Algorithm) and MD5 (Message Digest Algorithm 5) for generating fixed-size hash values from variable-size input data, useful for data integrity checks.
  4. Digital Signatures: Enables digital signing and verification using algorithms like RSA and DSA (Digital Signature Algorithm) to ensure the authenticity and integrity of messages or documents.
  5. Random Number Generation: Provides functionality for generating cryptographically secure random numbers, essential for creating secure keys and tokens.

Installation of pycryptodome in Python

Step 1: Create Environment

Create a virtual environment to manage project dependencies separately from the global Python installation. This helps prevent version conflicts and keeps the project isolated.

python -m venv myenv
1

Step 2: Activate Env

Activate the virtual environment to use its Python interpreter and installed packages. This ensures that any packages you install will be confined to this environment.

myenv\Scripts\activate
2

Step 3: Install pycryptodome

Install the pycryptodome library using pip. This command fetches the pycryptodome package from the Python Package Index (PyPI) and installs it in your virtual environment.

pip install pycryptodome
3-min

Step 4: Import pycryptodome

Import pycryptodome in your Python scripts to start using its cryptographic functions. Ensure that you do this within the scope of the virtual environment where pycryptodome is installed.

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

Example 1: Hashing with SHA-256

In this example, we are using the PyCryptodome library’s SHA256 module to create a SHA-256 hash of a byte string. We initialize the hash object, update it with the data, and then compute and print the hexadecimal representation of the resulting hash, providing a secure and unique hash for the given input.

Python
from Crypto.Hash import SHA256

hash_object = SHA256.new()

data = b'Hello GeeksforGeeks'
hash_object.update(data)
hash_hex = hash_object.hexdigest()
print(f"SHA-256 Hash: {hash_hex}")

Output:

SHA-256 Hash: 288353a10bcb0248dbf3af41c7fee3b4430f371b25ae273a5fc57d81576253b1

Example 2: Encrypting and Decrypting with AES

In this example, we are using PyCryptodome to perform AES encryption and decryption. We generate a random 16-byte key and initialization vector (IV), then use these to create an AES cipher object in CBC mode. The data is padded to ensure it fits the block size, encrypted, and then decrypted back to its original form, demonstrating secure encryption and decryption of data.

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

key = get_random_bytes(16)  
iv = get_random_bytes(16)   

cipher = AES.new(key, AES.MODE_CBC, iv)

data = b'Hello GeeksforGeeks'

padded_data = pad(data, AES.block_size)

ciphertext = cipher.encrypt(padded_data)

cipher_dec = AES.new(key, AES.MODE_CBC, iv)
decrypted_padded_data = cipher_dec.decrypt(ciphertext)

decrypted_data = unpad(decrypted_padded_data, AES.block_size)

print(f"Ciphertext: {ciphertext}")
print(f"Decrypted Data: {decrypted_data.decode('utf-8')}")

Output:

Ciphertext: b'I\xb9\x8d/\xc6\xce0J\x94l\xb4AJ\x0c\x081\xe7\x16\x80\x1e\xb3Q\xf0B\xab\x80\xf8Rl\xa2*\xd2'
Decrypted Data: Hello GeeksforGeeks

Conclusion

In conclusion, PyCryptodome is a powerful and useful library for cryptographic operations in Python, offering robust support for symmetric and asymmetric encryption, hashing, and digital signatures. Its ease of installation and comprehensive feature set make it an excellent choice for securing data and implementing cryptographic measures in Python applications




Reffered: https://www.geeksforgeeks.org


Python

Related
Why can't we access Python from a Conda Environment? Why can't we access Python from a Conda Environment?
How to Fix "TypeError: 'float' object is not callable" in Python How to Fix "TypeError: 'float' object is not callable" in Python
How to Fix 'No Module Named psycopg2' in Python AWS How to Fix 'No Module Named psycopg2' in Python AWS
Dynamic Forms Handling with HTMX and Python Flask Dynamic Forms Handling with HTMX and Python Flask
Serialize Python SqlAlchemy result to JSON Serialize Python SqlAlchemy result to JSON

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
22