A python xxxFile like ( ie GzipFile, BZ2File, ...) for manipulating AES encrypted files.
Project description
AesFile
A python xxxFile like (ie TarFile, GzipFile, BZ2File, pyzstd.ZstdFile, ...) for encrypting files with cryptography AES.
This project is part of the CofferFile : https://github.com/bibi21000/CofferFile
If you're looking for a more powerfull storage for your sensible datas, look at PyCoffer : https://github.com/bibi21000/PyCoffer.
Install
pip install aesfile
Create your encryption key
from Crypto.Random import get_random_bytes
key = get_random_bytes(16)
and store it in a safe place (disk, database, ...).
This key is essential to encrypt and decrypt data. Losing this key means losing the data.
"open" your encrypted files like normal files
Text files :
import aesfile
with aesfile.open('test.txc', mode='wt', aes_key=key, encoding="utf-8") as ff:
ff.write(data)
with aesfile.open('test.txc', "rt", aes_key=key, encoding="utf-8") as ff:
data = ff.read()
with aesfile.open('test.txc', mode='wt', aes_key=key, encoding="utf-8") as ff:
ff.writelines(data)
with aesfile.open('test.txc', "rt", aes_key=key, encoding="utf-8") as ff:
data = ff.readlines()
Binary files :
import aesfile
with aesfile.open('test.dac', mode='wb', aes_key=key) as ff:
ff.write(data)
with aesfile.open('test.dac', "rb", aes_key=key) as ff:
data = ff.read()
Or compress and crypt them with pyzstd
pip install aesfile[zstd]
from aesfile.zstd import AesFile
with AesFile('test.dac', mode='wb', aes_key=key) as ff:
ff.write(data)
with AesFile('test.dac', mode='rb', aes_key=key) as ff:
data = ff.read()
And chain it to tar and bz2
class TarBz2AesFile(tarfile.TarFile):
def __init__(self, name, mode='r', aes_key=None, chunk_size=aesfile.CHUNK_SIZE, **kwargs):
compresslevel = kwargs.pop('compresslevel', 9)
self.fernet_file = aesfile.AesFile(name, mode,
aes_key=aes_key, chunk_size=chunk_size, **kwargs)
try:
self.bz2_file = bz2.BZ2File(self.fernet_file, mode=mode,
compresslevel=compresslevel, **kwargs)
try:
super().__init__(fileobj=self.bz2_file, mode=mode, **kwargs)
except Exception:
self.bz2_file.close()
raise
except Exception:
self.fernet_file.close()
raise
def close(self):
try:
super().close()
finally:
try:
if self.fernet_file is not None:
self.bz2_file.close()
finally:
if self.fernet_file is not None:
self.fernet_file.close()
with TarBz2AesFile('test.zsc', mode='wb', aes_key=key) as ff:
ff.add(dataf1, 'file1.out')
ff.add(dataf2, 'file2.out')
with TarBz2AesFile('test.zsc', mode='rb', aes_key=key) as ff:
fdata1 = ff.extractfile('file1.out')
fdata2 = ff.extractfile('file2.out')
Encrypt / decrypt existing files
Encrypt :
import aesfile
with open(source, 'rb') as fin, aesfile.open(destination, mode='wb', aes_key=key) as fout:
while True:
data = fin.read(7777)
if not data:
break
fout.write(data)
Decrypt :
import aesfile
with aesfile.open(source, mode='rb', aes_key=key) as fin, open(destination, 'wb') as fout :
while True:
data = fin.read(8888)
if not data:
break
fout.write(data)
Or to compress and crypt
import aesfile.zstd
with open(source, 'rb') as fin, aesfile.zstd.open(destination, mode='wb', aes_key=key) as fout:
while True:
data = fin.read(7777)
if not data:
break
fout.write(data)
with aesfile.zstd.open(source, mode='rb', aes_key=key) as fin, open(destination, 'wb') as fout :
while True:
data = fin.read(8888)
if not data:
break
fout.write(data)
Look at documentation : https://bibi21000.github.io/AesFile/
Project details
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 aesfile-0.0.4.tar.gz.
File metadata
- Download URL: aesfile-0.0.4.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ebba31f7d939f192d90210e344ec287f8ecfd45cd69657a94289bfc03671c2f
|
|
| MD5 |
a86d96ddfcd68a46e85e5e625663ab60
|
|
| BLAKE2b-256 |
0eaa9ceac33738ed553647a86f4c8988af9a8be63290147ded6fb8b460bea620
|
Provenance
The following attestation bundles were made for aesfile-0.0.4.tar.gz:
Publisher:
python-publish.yml on bibi21000/AesFile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aesfile-0.0.4.tar.gz -
Subject digest:
3ebba31f7d939f192d90210e344ec287f8ecfd45cd69657a94289bfc03671c2f - Sigstore transparency entry: 173883194
- Sigstore integration time:
-
Permalink:
bibi21000/AesFile@81352c9532e2be1849e2ac40f0d66c7329372160 -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/bibi21000
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@81352c9532e2be1849e2ac40f0d66c7329372160 -
Trigger Event:
release
-
Statement type:
File details
Details for the file aesfile-0.0.4-py3-none-any.whl.
File metadata
- Download URL: aesfile-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59185cab4ba0c95a77c28a3c066a0424811219287df45ec32277551b4de1392d
|
|
| MD5 |
504eee2f72db313e58ae5c44ba8ee484
|
|
| BLAKE2b-256 |
b042fce6a0161503f5e60335ef9b98024762b47cc5d6e1ad35fc81acd6609d8c
|
Provenance
The following attestation bundles were made for aesfile-0.0.4-py3-none-any.whl:
Publisher:
python-publish.yml on bibi21000/AesFile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aesfile-0.0.4-py3-none-any.whl -
Subject digest:
59185cab4ba0c95a77c28a3c066a0424811219287df45ec32277551b4de1392d - Sigstore transparency entry: 173883197
- Sigstore integration time:
-
Permalink:
bibi21000/AesFile@81352c9532e2be1849e2ac40f0d66c7329372160 -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/bibi21000
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@81352c9532e2be1849e2ac40f0d66c7329372160 -
Trigger Event:
release
-
Statement type: