![]() |
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
Installation of pycryptodome in PythonStep 1: Create EnvironmentCreate 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 ![]() Step 2: Activate EnvActivate 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 ![]() Step 3: Install pycryptodomeInstall 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 ![]() Step 4: Import pycryptodomeImport 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.
Example 1: Hashing with SHA-256In 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.
Output: SHA-256 Hash: 288353a10bcb0248dbf3af41c7fee3b4430f371b25ae273a5fc57d81576253b1 Example 2: Encrypting and Decrypting with AESIn 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.
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 ConclusionIn 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 |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 22 |