This Python script provides a Crypt
class that allows you to perform AES encryption and decryption using PBKDF2-based key derivation. It employs the cryptography
library to ensure secure and efficient encryption and decryption of sensitive data.
- AES encryption and decryption with PBKDF2-based key derivation
- Randomly generated salt and IV (Initialization Vector) for increased security
- Padding to ensure that the data being encrypted or decrypted is a multiple of the block size
- Python 3.x
cryptography
library (Install usingpip install cryptography
)
To encrypt data using the Crypt
class, follow these steps:
- Import the
Crypt
class from the script. - Initialize the
Crypt
class. - Use the
encrypt
method to encrypt the data.
from crypt import Crypt
word = "ARandomSentence"
key = "your_key" # Replace with your encryption key
encrypted_word = Crypt.encrypt(word, key)
print("Encrypted word:", encrypted_word)
To decrypt data using the Crypt class, follow these steps:
- Import the Crypt class from the script.
- Initialize the Crypt class.
- Use the decrypt method to decrypt the data.
from crypt import Crypt
encrypted_word = "your_encrypted_word" # Replace with your encrypted data
key = "your_key" # Replace with your encryption key
decrypted_word = Crypt.decrypt(encrypted_word, key)
print("Decrypted word:", decrypted_word)
For a complete example, refer to the provided example.py script.
The script also includes utility functions for demonstrating the usage of the Crypt class. These utilities showcase how to encrypt and decrypt data, and how to handle exceptions.
To use the utilities, uncomment the desired section and replace the example values with your own.
The script provides error handling examples within the utilities. When using the Crypt class, it's important to handle exceptions that may arise during encryption and decryption, such as incorrect keys or corrupted data.
This script is provided under the MIT License. You are free to modify and use it for your own purposes.