def download(filename, destination, privpath, user): """Download function, creates a temporary file to where the encrypted file is streamed to, then it is read chunk by chunk and decrypted. Security: - Authentication - File is decrypted with AES - hasher is created for File Integrity Control - Public key is accessed for decrypting the AES key - Private Key is accessed for verifying the file signature - All relevant information is kept client-side""" (username, session) = user.getInfo() message = {'filename': filename} messageToSend = security.encryptS_AES(json.dumps(message), session.decode('hex')).encode('hex') params = {'data': messageToSend, 'username': username} datagen, headers = multipart_encode(params) try: resp = urllib2.Request('https://localhost:8080/download', datagen, headers) data = urllib2.urlopen(resp) fn = data.info().getheader('filename') date = json.loads( security.decryptS_AES(data.info().getheader('data').decode('hex'), session.decode('hex'))) aes = data.info().getheader('aes').decode('hex') iv = date['iv'].decode('hex') signature = date['sign'].decode('hex') with open(privpath, 'rb') as f: priv = security.importkey_RSA(f.read()) pub = security.importkey_RSA(getPubKey(username)) RsaAES = security.decrypt_RSA(priv, aes) decipher = security.getDecipher(iv, RsaAES) tf = tempfile.NamedTemporaryFile(delete=True) CHUNK = 16 * 1024 while True: chunk = data.read(CHUNK) if not chunk: break tf.write(chunk) tf.seek(0) hasher = security.Hasher() with open(os.path.join(str(destination), filename), 'wb') as out: security.decrypt_AES(decipher, tf, out, hasher) new = hasher.get() if security.verifyFile(pub, new, signature): print 'The File was not changed!' else: print 'The File was changed!' tf.close() except urllib2.HTTPError as e: print str(e.code) + ': ' + e.reason print 'Currently, you are not a valid user!\nSafeBox Team'
def download(filename, destination, privpath, user): """Download function, creates a temporary file to where the encrypted file is streamed to, then it is read chunk by chunk and decrypted. Security: - Authentication - File is decrypted with AES - hasher is created for File Integrity Control - Public key is accessed for decrypting the AES key - Private Key is accessed for verifying the file signature - All relevant information is kept client-side""" (username, session) = user.getInfo() message = {"filename": filename} messageToSend = security.encryptS_AES(json.dumps(message), session.decode("hex")).encode("hex") params = {"data": messageToSend, "username": username} datagen, headers = multipart_encode(params) try: resp = urllib2.Request("https://localhost:8080/download", datagen, headers) data = urllib2.urlopen(resp) fn = data.info().getheader("filename") date = json.loads(security.decryptS_AES(data.info().getheader("data").decode("hex"), session.decode("hex"))) aes = data.info().getheader("aes").decode("hex") iv = date["iv"].decode("hex") signature = date["sign"].decode("hex") with open(privpath, "rb") as f: priv = security.importkey_RSA(f.read()) pub = security.importkey_RSA(getPubKey(username)) RsaAES = security.decrypt_RSA(priv, aes) decipher = security.getDecipher(iv, RsaAES) tf = tempfile.NamedTemporaryFile(delete=True) CHUNK = 16 * 1024 while True: chunk = data.read(CHUNK) if not chunk: break tf.write(chunk) tf.seek(0) hasher = security.Hasher() with open(os.path.join(str(destination), filename), "wb") as out: security.decrypt_AES(decipher, tf, out, hasher) new = hasher.get() if security.verifyFile(pub, new, signature): print "The File was not changed!" else: print "The File was changed!" tf.close() except urllib2.HTTPError as e: print str(e.code) + ": " + e.reason print "Currently, you are not a valid user!\nSafeBox Team"
def shareFile(user, filename, usr_dest, permission): """Share function, allows a file to be shared with another user. The file with the AES used to encrypt the file to be shared is sent from the server, it is decrypted and encrypted with the target user's public key Security: - Sharing user's private key is used for decrypting file with AES key - Target user's public key is used for encrypting file with AES key - Sharing client receives no information from the other user except username and public key - All relevant information is kept client-side""" (username, session) = user.getInfo() message = { 'filename': filename, 'usrdstname': usr_dest, } messageToSend = security.encryptS_AES(json.dumps(message), session.decode('hex')).encode('hex') params = {'data': messageToSend, 'username': username} datagen, headers = multipart_encode(params) try: resp = urllib2.Request('https://localhost:8080/share', datagen, headers) data = urllib2.urlopen(resp) date = json.loads( security.decryptS_AES(data.info().getheader('data').decode('hex'), session.decode('hex'))) aes = date['aes'].decode('hex') pub_key = date['pubkey'].decode('hex') with open('PrivateKeys/Private_key_' + str(username), 'rb') as f: priv = security.importkey_RSA(f.read()) RsaAES = security.decrypt_RSA(priv, aes) tf = tempfile.NamedTemporaryFile(delete=True) security.encrypt_RSA(security.importkey_RSA(pub_key), RsaAES, tf) message = { 'filename': filename, 'usrdstname': usr_dest, 'filekey': tf.read().encode('hex'), 'permission': permission } messageToSend = security.encryptS_AES( json.dumps(message), session.decode('hex')).encode('hex') request = urllib2.Request('https://localhost:8080/shareFile') request.add_header('username', username) request.add_header('data', messageToSend) response = urllib2.urlopen(request) tf.close() print response.read() except urllib2.HTTPError as e: print str(e.code) + ': ' + e.reason print 'Currently, you are not a valid user!\nSafeBox Team'
def diff(user, filename, privpath, filefp): """Function for getting the difference between a file in the server and a user file, works like download except no file is written. Security: - Authentication - File is decrypted with AES - hasher is created for File Integrity Control - Public key is accessed for decrypting the AES key - Private Key is accessed for verifying the file signature - All relevant information is kept client-side" """ (username, session) = user.getInfo() message = {"filename": filename} messageToSend = security.encryptS_AES(json.dumps(message), session.decode("hex")).encode("hex") params = {"data": messageToSend, "username": username} datagen, headers = multipart_encode(params) try: resp = urllib2.Request("https://localhost:8080/download", datagen, headers) data = urllib2.urlopen(resp) fn = data.info().getheader("filename") date = json.loads(security.decryptS_AES(data.info().getheader("data").decode("hex"), session.decode("hex"))) aes = data.info().getheader("aes").decode("hex") iv = date["iv"].decode("hex") signature = date["sign"].decode("hex") with open(privpath, "rb") as f: priv = security.importkey_RSA(f.read()) pub = security.importkey_RSA(getPubKey(username)) RsaAES = security.decrypt_RSA(priv, aes) decipher = security.getDecipher(iv, RsaAES) tf = tempfile.NamedTemporaryFile(delete=True) out = tempfile.NamedTemporaryFile(delete=True) CHUNK = 16 * 1024 while True: chunk = data.read(CHUNK) if not chunk: break tf.write(chunk) tf.seek(0) hasher = security.Hasher() security.decrypt_AES(decipher, tf, out, hasher) out.seek(0) new = hasher.get() comp = open(filefp, "rb").readlines() cenas = out.readlines() return diffchecker(comp, cenas) except urllib2.HTTPError as e: print str(e.code) + ": " + e.reason print "Currently, you are not a valid user!\nSafeBox Team"
def fileList(user): """Function for listing user files, handled server side since no security measures other than Authentication are needed""" register_openers().add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) (username, session) = user.getInfo() params = {"username": username} try: datagen, headers = multipart_encode(params) resp = urllib2.Request("https://localhost:8080/listMyFiles", datagen, headers) files = urllib2.urlopen(resp).read() list_files = json.loads(security.decryptS_AES(files.decode("hex"), session.decode("hex"))) return list_files except urllib2.HTTPError as e: print str(e.code) + ": " + e.reason print "Currently, you are not a valid user!\nSafeBox Team" return []
def shareFile(user, filename, usr_dest, permission): """Share function, allows a file to be shared with another user. The file with the AES used to encrypt the file to be shared is sent from the server, it is decrypted and encrypted with the target user's public key Security: - Sharing user's private key is used for decrypting file with AES key - Target user's public key is used for encrypting file with AES key - Sharing client receives no information from the other user except username and public key - All relevant information is kept client-side""" (username, session) = user.getInfo() message = {"filename": filename, "usrdstname": usr_dest} messageToSend = security.encryptS_AES(json.dumps(message), session.decode("hex")).encode("hex") params = {"data": messageToSend, "username": username} datagen, headers = multipart_encode(params) try: resp = urllib2.Request("https://localhost:8080/share", datagen, headers) data = urllib2.urlopen(resp) date = json.loads(security.decryptS_AES(data.info().getheader("data").decode("hex"), session.decode("hex"))) aes = date["aes"].decode("hex") pub_key = date["pubkey"].decode("hex") with open("PrivateKeys/Private_key_" + str(username), "rb") as f: priv = security.importkey_RSA(f.read()) RsaAES = security.decrypt_RSA(priv, aes) tf = tempfile.NamedTemporaryFile(delete=True) security.encrypt_RSA(security.importkey_RSA(pub_key), RsaAES, tf) message = { "filename": filename, "usrdstname": usr_dest, "filekey": tf.read().encode("hex"), "permission": permission, } messageToSend = security.encryptS_AES(json.dumps(message), session.decode("hex")).encode("hex") request = urllib2.Request("https://localhost:8080/shareFile") request.add_header("username", username) request.add_header("data", messageToSend) response = urllib2.urlopen(request) tf.close() print response.read() except urllib2.HTTPError as e: print str(e.code) + ": " + e.reason print "Currently, you are not a valid user!\nSafeBox Team"
def getSharedWith(user, filename): """Function for getting the usernames that have a certain user's file being shared with them, handled server side since no security measures other than Authentication are needed""" register_openers().add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) (username, session) = user.getInfo() message = {"filename": filename} messageToSend = security.encryptS_AES(json.dumps(message), session.decode("hex")).encode("hex") params = {"username": username, "data": messageToSend} try: datagen, headers = multipart_encode(params) resp = urllib2.Request("https://localhost:8080/getSharedWith", datagen, headers) response = urllib2.urlopen(resp).read() users = json.loads(security.decryptS_AES(response.decode("hex"), session.decode("hex"))) list_users = [x.encode("latin-1") for x in users] return list_users except urllib2.HTTPError as e: print str(e.code) + ": " + e.reason print "Currently, you are not a valid user!\nSafeBox Team" return []