This commit is contained in:
Luke Ogburn
2021-03-02 16:31:04 -05:00
commit 4e23acb940
4 changed files with 163 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
videos/encrypted/*
videos/plaintext/*
keys/*

88
cronjob.py Normal file
View File

@@ -0,0 +1,88 @@
'''
This script records videos of a particular length and then takes the normal file and encrypts it,
storing both the encrypted file and the randomly generated iv (and deleting the plaintext video
file). It is designed to be called every x seconds, where x is the length of the desired video.
If it is not, then the resulting videos will have gaps between them.
'''
# import things
from cryptography.fernet import Fernet
from Crypto.Cipher import PKCS1_OAEP as RSA
from Crypto.Cipher import AES
import os
import glob
from signal import signal, SIGINT
from goprocam import GoProCamera, constants
from base64 import b64decode, b64encode
# global vars
location = "/home/pi/final/videos/" # location of video files, subfolders "plaintext" and "encrypted" assumed to exist
seconds = 3 # seconds to record video for
# helps with video recording
def handler(s, f):
gopro.stopWebcam()
quit()
# returns the current file number for naming convention
def numFiles():
return str(len([item for item in os.listdir(location+"encrypted/") if os.path.isfile(os.path.join(location+"encrypted/", item))]))
# take video for time seconds, transfer to local storage and delete from gopro
def takeVideo(time):
signal(SIGINT, handler)
gopro = GoProCamera.GoPro(ip_address=GoProCamera.GoPro.getWebcamIP())
gopro.shoot_video(time)
gopro.downloadLastMedia(custom_filename=(location+"plaintext/" + numFiles() + ".mp4"))
gopro.delete("last")
# TODO encrypt file data with assumed pgp key (file should already be read, this essentially
# encrypts the text or contents of the file)
def encryptPGP(file):
return file
#with open("pubkey.pem", "r") as keyfile:
# key = RSA.importKey(b64decode(str(keyfile)))
keytext = "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLjon5dPwTgHUr44KndKz0W7bN\nwEqs6IImvnh/X++FD8vpMNPpepgEpZBC52vTyhmsgJYbkNnp8DxSUApE+IL5FlKr\nDCTLp3KLfjD8onjHf7mtD7uMpxWkafmor4BGq05M/QRwttYjaQNBGT3BFYHi5NdX\negPZLolmwrqwndwGAQIDAQAB\n-----END PUBLIC KEY-----"
key = RSA.importKey(keytext)
return key.encrypt(file, 'x')[0]
# encrypt given (video) file and store both encrypted video and (TODO) encrypted random iv
def encryptVideo(file):
# open the file as bytes
with open(file, "rb") as og:
message = og.read()
# generate keys
key = Fernet.generate_key()
fernet = Fernet(key)
# encrypt file
enc = fernet.encrypt(message)
# save the encrypted mp4
with open(location+"encrypted/" + numFiles() + ".mpc", "wb") as encf:
encf.write(enc)
# save the key
with open("keys/" + numFiles() + ".asc", "wb") as keyf:
keyf.write(encryptPGP(key))
# record the video and store in file location
takeVideo(seconds)
# get all video files in folder, encrypt (get all just in case one was missed previously)
fileset = [file for file in glob.glob(location + "plaintext/*.mp4", recursive=False)]
# whether or not to print out current action
verbose = True
for file in fileset:
if verbose:
print("Working on file " + file)
print("Encrypting...")
encryptVideo(file)
if verbose:
print("Deleting...")
os.remove(file)
if verbose:
print("Done.")

9
pubkey.pem Normal file
View File

@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuk5egzauVPCxuNvWcJAA
ISIxcvsdXQFFB5lYUdOgv8Dcyva/9kh76R2TEay0K3kEpXBx/M4YdiAIg0XoTYUx
WNR3vGy4sWuAkF1WPF0hCJcx/GGCCszl7NqMDz7uedBSkJG4sIyOCnmxJli/Y7O+
xoENb1DvJ4U7b0n3AHkovlsfkw8zwOVsc9dUnPbZmrzbd99HYoRFPKnAVgF+vGY6
2qRj2GvsJzRAC0K8HMxJv0r/V+3O+fwqGCnJlCcadgMJUuHPDVdYVXgEZZ1iYdwF
pPHLML2DEOd3y3DJAy1F59IkEGArULQbu7SdqwaFkU1Y2euZXb/J1q/9C/GhppQH
9QIDAQAB
-----END PUBLIC KEY-----

63
transfer.py Normal file
View File

@@ -0,0 +1,63 @@
import socket
import sys
import netifaces
import time
import glob
# global vars
HEADERSIZE = 20
# helper functions
def get_ip():
return [l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
if not ip.startswith("127.")][:1], [[(s.connect(('1.1.1.1', 53)),
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
done = True
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '10.25.10.18' # IP of server that will recieve, not this device
port = 12352
location = "/home/pi/final/"
vidset = [file for file in glob.glob(location + "videos/encrypted/*", recursive=False)]
keyset = [file for file in glob.glob(location + "keys/*", recursive=False)]
if len(vidset) != len(keyset):
print("Video and keyfiles are not properly matched")
exit()
def is_interface_up(interface):
addr = netifaces.ifaddresses(interface)
return netifaces.AF_INET in addr
while done:
if is_interface_up('wlan0'):
IPAddr = netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr']
print(IPAddr)
if IPAddr == get_ip():
#try:
s.connect((host, port))
for i in range(len(vidset)):
# send video
viddata = open(vidset[i], 'r').read()
viddata = f"{len(viddata):<{HEADERSIZE}}" + viddata
s.send(bytes(viddata, "utf-8"))
#send key
keydata = open(keyset[i], 'r').read()
keydata = f"{len(keydata):<{HEADERSIZE}}" + keydata
s.send(bytes(keydata, "utf-8"))
print("Sent data (" + str(i+1) + "/" + str(len(vidset)) + ")")
done = False
#except:
#host = input("(WIP, just press enter and change var) If host is not at ip {host}, please type in host v4 IP. Otherwise press enter to abort.")
#done = False
else:
print('IP address of client (this device) is undetermined.')
time.sleep(120)
else:
print('No connection, please allow this device to access local router or internet. Attempting connection again in 5 seconds. Press Ctrl+C to stop exit.')
time.sleep(5)
print('Done.')