def ShareKey(self, request, context): Ticket = request.file2.encode() fernet = Fernet(self.kdcKey) plaintext = fernet.decrypt(Ticket).decode().split() shared_key, pid = plaintext self.clientCount += 1 if self.session != ("", ""): print("Session over!\n") self.session = ("", "") print("\n{} )Received a session key from client pid {} with a nonce.". format(self.clientCount, pid)) shared_key = shared_key.encode() self.sessionKeys[pid] = shared_key self.session = (pid, shared_key.decode()) self.sessionCount = 0 fernet = Fernet(shared_key) plaintext = str( int(fernet.decrypt(request.file1.encode()).decode()) - 1) ciphertext = fernet.encrypt(plaintext.encode()).decode() print( " Sent a nonce to the client for authentication.\n Connection has been established!\n" ) return filepb.Response(name=ciphertext)
def PWD(self, request, context): fernet = Fernet(self.session[1].encode()) self.sessionCount += 1 print(" {}) PWD request from Client with PID {}.".format( self.sessionCount, self.session[0])) ciphertext = fernet.encrypt((self.directory).encode()).decode() return filepb.Response(name=ciphertext)
def CAT(self, request, context): fernet = Fernet(self.session[1].encode()) self.sessionCount += 1 print(" {}) CAT request from Client with PID {}.".format( self.sessionCount, self.session[0])) plaintext = fernet.decrypt(request.name.encode()).decode() if os.path.isfile(os.path.join(self.directory, plaintext)) == 1: with open(os.path.join(self.directory, plaintext), 'r') as f: content = f.read() else: content = "N" ciphertext = fernet.encrypt((content).encode()).decode() return filepb.Response(name=ciphertext)
def LS(self, request, context): fernet = Fernet(self.session[1].encode()) self.sessionCount += 1 List = os.listdir(self.directory) print(" {}) LS request from Client with PID {}.".format( self.sessionCount, self.session[0])) # Pb = filepb.Files() plaintext = "" for l in List: plaintext += l + " " # f = Pb.file.add() # f.n = l if len(plaintext) > 0: plaintext = plaintext[:-1] ciphertext = fernet.encrypt(plaintext.encode()).decode() return filepb.Response(name=ciphertext)
def CP(self, request, context): fernet = Fernet(self.session[1].encode()) file1, file2 = fernet.decrypt( request.name.encode()).decode().split(" ") self.sessionCount += 1 print(" {}) CP request from Client with PID {}.".format( self.sessionCount, self.session[0])) if os.path.isfile( os.path.join(self.directory, file1) ) == 1: #and os.path.isfile(os.path.join(self.directory, file2)) == 1: with open(os.path.join(self.directory, file1), 'r') as f: content = f.read() with open(os.path.join(self.directory, file2), 'a') as f: f.write("\n" + content) with open(os.path.join(self.directory, file2), 'r') as f: content = f.read() else: content = "N" content = fernet.encrypt((content).encode()).decode() return filepb.Response(name=content)
def NEW(self, request, context): self.sessionCount += 1 fernet = Fernet(self.session[1].encode()) plaintext = fernet.decrypt(request.name.encode()).decode() fer = Fernet(self.kdcKey) ciphertext = fer.encrypt( (plaintext + " " + self.session[0]).encode()).decode() msg = "FS" + self.port + " " + ciphertext channel = grpc.insecure_channel('localhost:50051') stub = central_grpc.CentralStub(channel) stub.NewFile(centralpb.Request(name=msg)) content = "File has been created by client with PID " + self.session[0] with open(os.path.join(self.directory, plaintext), 'w') as f: f.write(content) print( " {}) NEW request from Client with PID {}. Submitted the update to Central Server." .format(self.sessionCount, self.session[0])) ciphertext = fernet.encrypt((content).encode()).decode() return filepb.Response(name=ciphertext)