Compress and Crypt
This commit is contained in:
parent
7e2a7046a5
commit
274711a22d
11
compress.py
11
compress.py
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
24
crypt.py
24
crypt.py
@ -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,
|
||||||
|
21
main.py
21
main.py
@ -1,17 +1,24 @@
|
|||||||
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 *
|
||||||
|
|
||||||
key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits)
|
if not os.path.exists("key"):
|
||||||
print(key)
|
key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits)
|
||||||
|
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'
|
||||||
|
|
||||||
encrypt_file(key, file, cfile)
|
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")
|
Loading…
x
Reference in New Issue
Block a user