A simple data encryption-decryption package for Django models.
Project description
# data_encrypter
A simple Python package for encrypting and decrypting Django model instances using a password-derived key. This package uses XOR encryption, base64 encoding, and pickling for serialization.
**Important Security Note:** This implementation is for demonstration and educational purposes. For production systems, **strongly consider** using robust encryption libraries (like `cryptography`) and proper key management solutions. XOR encryption is not considered secure for sensitive data in real-world applications.
## Installation
```bash
pip install data_encrypter
Dependencies
This package requires Django. It will be automatically installed when you install data_encrypter.
Usage
-
Import:
from data_encrypter import DataEncrypter
-
Create an instance of the encrypter:
encrypter = DataEncrypter()
-
Encrypt a model instance:
from myapp.models import MyModel # Replace with your model instance = MyModel.objects.get(pk=1) # Get the instance you want to encrypt password = "your_secret_password" # **Use a strong, unique password!** encrypter.encrypt_model_instance(instance, password)
-
Decrypt a model instance:
from myapp.models import MyModel # Replace with your model instance = MyModel.objects.get(pk=1) # Get the instance you want to decrypt password = "your_secret_password" # **Must be the same password used for encryption!** decrypted_fields = encrypter.decrypt_model_instance(instance, password) # Access the decrypted values (they are now on the instance) print(instance.some_field) print(instance.another_field) # ... and so on
Important: The decrypt_model_instance method does not automatically save the decrypted data back to the database. If you want to save the changes, you need to call instance.save() yourself after decrypting. For example:
decrypted_fields = encrypter.decrypt_model_instance(instance, password)
instance.save(update_fields=decrypted_fields) # Save the changes
Example
from data_encrypter import DataEncrypter
from myapp.models import MyModel
encrypter = DataEncrypter()
password = "my_strong_password"
# Create a model instance (or get an existing one)
instance = MyModel(some_field="Some Value", another_field="Another Value")
instance.save()
# Encrypt
encrypter.encrypt_model_instance(instance, password)
print("Encrypted instance:", instance.some_field, instance.another_field) # These will be encrypted
# ... later, retrieve the instance from the database ...
instance = MyModel.objects.get(pk=instance.pk) # Refresh from DB
# Decrypt
encrypter.decrypt_model_instance(instance, password)
print("Decrypted instance:", instance.some_field, instance.another_field) # These will be decrypted
instance.save(update_fields=['some_field', 'another_field']) # Save the changes
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
MIT License (See LICENSE file for details)
This version keeps all the information within the same README.md file, without referring to separate modules or instructions. It's self-contained and ready to be used as your README.md content.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file data_encrypter-1.0.tar.gz.
File metadata
- Download URL: data_encrypter-1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ef361b27295f2e656e616ea93d9fb935c269e6fa724fa7a8ae93a0b3f13e405
|
|
| MD5 |
8592c55e29845b8a613e24833861ed82
|
|
| BLAKE2b-256 |
be45c76dec0a07202a72a38783a1e4d56eb38086ed9a5ea8d40eeb5ad9113d85
|
File details
Details for the file data_encrypter-1.0-py3-none-any.whl.
File metadata
- Download URL: data_encrypter-1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b05399a76a45a30762a7e6fac79bbebfec7f32cb95988904bba1b95a54e21d7
|
|
| MD5 |
0cffbfd515e9cd4670df6fb8a8b45905
|
|
| BLAKE2b-256 |
0f316e84da43e796db35469eeba37198c1332ca786576f48261e79a23b55cbbb
|