added compression

This commit is contained in:
luke
2021-03-17 11:58:42 -04:00
parent c1a86adcf5
commit 7dba27f1e6

View File

@@ -2,6 +2,7 @@ import socket
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP as pkrsa
from Crypto.Cipher import AES
import zipfile
import os
import glob
import time
@@ -9,6 +10,7 @@ import requests
# global vars
endmessage = "---endmessage---"
servername = "http://10.25.10.177:63654"
HEADERSIZE = 20
BYTELEN = 1024
BYTELENBIG = 4096
@@ -20,25 +22,12 @@ def get_ip():
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
def getStream(bytelen):
newmsg = True
done = False
fullmsg = ""
while not done:
msg = c.recv(bytelen)
if newmsg:
msglen = int(str(msg[:HEADERSIZE].decode("utf-8")))
print(str(msglen))
newmsg = False
fullmsg += msg.decode("utf-8")
if len(fullmsg) - HEADERSIZE == msglen:
print(f"{len(fullmsg) - HEADERSIZE} : {msglen}")
done = True
return fullmsg[HEADERSIZE:]
else:
if len(fullmsg) - HEADERSIZE > msglen:
done = True
print(f"{len(fullmsg) - HEADERSIZE} | {msglen}")
# returns the decompressed file
def decompress(filename):
zf = zipfile.ZipFile(filename, mode='r', compression=zipfile.ZIP_DEFLATED)
internalfile = zf.namelist()[0]
with open(filename[:-4], "wb+") as returnfile:
returnfile.write(zf.read(internalfile))
def decryptRSA(data):
key = RSA.import_key(open("privatekey.pem").read())
@@ -62,8 +51,9 @@ def decryptSet(keyset, vidset, verbose=False):
key = PKdecrypt(key)
cipher = AES.new(key, AES.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
with open(location + "/" + str(i+1) + ".mp4", "wb+") as mp:
mp.write(data)
with open(location + "/" + str(i+1) + ".mp4.zip", "wb+") as zp:
zp.write(data)
decompress(location + "/" + str(i+1) + ".mp4.zip")
if verbose:
print(f"Decrypted file ({i+1}/{len(vidset)})")
@@ -82,85 +72,15 @@ def downloadFromServer(urlWithPort, verbose=False):
if verbose:
print(f"Recieved ({i+1}/{int(r.content)})")
# establish connection with client and download files
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = get_ip()
print(host)
port = 12352
s.bind((host, port))
s.listen(5)
while True:
'''
OBSOLETE
# initial vars, connect with client device
c, addr = s.accept()
print(f'Got connection from {addr}')
# get number of files and recieve that num of files
numFiles = 2 # int(c.recv(80).decode()) TODO
for i in range(numFiles):
# get video and key data from remote, store both in respective files
open("videos/encrypted/" + str(i+1) + ".mpc", "w+").write(getStream(BYTELEN))
open("keys/" + str(i+1) + ".asc", "w+").write(getStream(BYTELEN))
# update user because this might take some time
print("Recieved data (" + str(i+1) + "/" + str(numFiles) + ")")
c.close()
# decrypt the files
print("Decrypting files...")
# get all video and key files in array (strings of file locations)
vidset = [file for file in glob.glob("videos/encrypted/*", recursive=False)]
keyset = [file for file in glob.glob("keys/*", recursive=False)]
location = "videos/plaintext/" + str(int(time.time()))
os.mkdir(location)
for i in range(len(vidset)):
# read the key and video file into vars
key = bytes(open(keyset[i], "r").read(), 'utf-8')
text = bytes(str(open(vidset[i], "r").read()), 'utf-8')
# write the decrypted video file info into an mp4 file
with open(location + "/" + str(i+1) + ".mp4", "wb+") as mp4:
mp4.write(decryptText(text, key))
# delete the old files
#os.remove(vidset[i])
#os.remove(keyset[i])
print(f"(dev mode) Would have deleted {vidset[i]} and {keyset[i]}")
'''
# download all videos
downloadFromServer("http://10.25.10.125:63655")
downloadFromServer(servername)
# get all video and key files in array (strings of file locations)
vidset = [file for file in glob.glob("videos/encrypted/*", recursive=False)]
keyset = [file for file in glob.glob("keys/*", recursive=False)]
decryptSet(keyset, vidset)
'''
location = "videos/plaintext/" + str(int(time.time()))
os.mkdir(location)
for i in range(len(vidset)):
file_in = open(vidset[i], "rb")
nonce, tag, ciphertext = [ file_in.read(x) for x in (16, 16, -1) ]
key = open(keyset[i], "rb")
cipher = AES.new(key.read(), AES.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
with open(location + "/" + str(i+1) + ".mp4", "wb+") as mp:
mp.write(data)
'''
'''
url = 'http://10.25.10.125:63655/numFiles.txt'
r = requests.get(url, allow_redirects=True)
print("Downloading files...")
for i in range(int(r.content)):
url = "http://10.25.10.125:63655/keys/" + str(i+1) + ".asc"
with open("keys/" + str(i+1) + ".asc", "wb+") as keyf:
keyf.write(requests.get(url, allow_redirects=True).content)
url = "http://10.25.10.125:63655/videos/encrypted/" + str(i+1) + ".mpc"
with open("videos/encrypted/" + str(i+1) + ".mpc", "wb+") as vidf:
vidf.write(requests.get(url, allow_redirects=True).content)
print(f"Recieved ({i+1}/{int(r.content)})")
'''
print('Done')
exit()