Compress and Crypt

This commit is contained in:
toto 2021-09-21 16:36:13 +02:00
parent 7e2a7046a5
commit 274711a22d
3 changed files with 51 additions and 21 deletions

View File

@ -1,6 +1,8 @@
import os import os
import re import re
import gzip import gzip
import tempfile
from crypt import *
def recurse(path, maxsize="50M"): def recurse(path, maxsize="50M"):
@ -12,17 +14,20 @@ def recurse(path, maxsize="50M"):
Hsize = str(human_readable_size(size, 0)) Hsize = str(human_readable_size(size, 0))
print(f + " : " + Hsize) print(f + " : " + Hsize)
if size > max: if size > max:
compress(uri) compress(uri, key)
elif os.path.isdir(uri): elif os.path.isdir(uri):
recurse(uri) recurse(uri)
def compress(file): def compress(file, key):
gz = tempfile.SpooledTemporaryFile()
with open(file, 'rb') as infile: with open(file, 'rb') as infile:
with gzip.open(file + ".gz", 'wb') as zipfile: with gzip.open(gz, 'wb') as zipfile:
while chunk := infile.read(64 * 1024): while chunk := infile.read(64 * 1024):
zipfile.write(chunk) zipfile.write(chunk)
print(file + " compressed") print(file + " compressed")
encrypt_file(key, gz, file + ".gz.enc")
print(file + " encrypted")
pass pass

View File

@ -1,6 +1,11 @@
import os, random, struct import io
import os
import struct
import tempfile
from Crypto.Cipher import AES from Crypto.Cipher import AES
def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024): def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024):
""" Encrypts a file using AES (CBC mode) with the """ Encrypts a file using AES (CBC mode) with the
given key. given key.
@ -22,13 +27,25 @@ def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024):
sizes can be faster for some files and machines. sizes can be faster for some files and machines.
chunksize must be divisible by 16. chunksize must be divisible by 16.
""" """
if not out_file: if not out_file:
out_file = in_file + '.enc' out_file = in_file + '.enc'
cipher = AES.new(key, AES.MODE_CBC) cipher = AES.new(key, AES.MODE_CBC)
filesize = os.path.getsize(in_file)
with open(in_file, 'rb') as infile: if type(in_file) is str:
filesize = os.path.getsize(in_file)
infile = open(in_file, 'rb')
elif type(in_file) is io.BufferedRandom or tempfile.SpooledTemporaryFile:
in_file.seek(0, 2)
filesize = in_file.tell()
in_file.seek(0)
infile = in_file
else:
print("Unable to encrypt " + str(in_file) + " of type " + str(type(in_file)))
return
with open(out_file, 'wb') as outfile: with open(out_file, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize)) outfile.write(struct.pack('<Q', filesize))
outfile.write(cipher.iv) outfile.write(cipher.iv)
@ -39,6 +56,7 @@ def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024):
outfile.write(cipher.encrypt(chunk)) outfile.write(cipher.encrypt(chunk))
def decrypt_file(key, in_file, out_file=None, chunksize=24 * 1024): def decrypt_file(key, in_file, out_file=None, chunksize=24 * 1024):
""" Decrypts a file using AES (CBC mode) with the """ Decrypts a file using AES (CBC mode) with the
given key. Parameters are similar to encrypt_file, given key. Parameters are similar to encrypt_file,

15
main.py
View File

@ -1,11 +1,16 @@
from crypt import *
from Crypto.Random import get_random_bytes from Crypto.Random import get_random_bytes
from crypt import *
from compress import * from compress import *
if not os.path.exists("key"):
key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits) key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits)
print(key) open("key", 'wb').write(key)
print("Saved")
else:
key = open("key", 'rb').read()
print("Recovered")
print(key)
file = "original_file.txt" file = "original_file.txt"
cfile = file + '.enc' cfile = file + '.enc'
@ -14,4 +19,6 @@ encrypt_file(key, file, cfile)
decrypt_file(key, cfile) decrypt_file(key, cfile)
rootdir = "test" rootdir = "test"
recurse(rootdir) recurse(rootdir, key)
decrypt_file(key, "test/DressLens4.mp4.gz.enc", "test/DressLens4.mp4.gz")