def decrypt(self,AES_KEY): """ Wrapper to Decrypt file. """ Common.decrypt_file(AES_KEY,'decrypted.enc')
def do_get(self,line): """ Gets the file from the server!! line: filename : The filename to be retrieved. <encflag> : "E" or "N", whether the file was encrypted. <opt password> : Password<8 Characters> for decrypting the file. """ args = line.split(" ") if len(args)==2: #case get <filename> <encflag = N> filename, encflag = line.split(" ") if encflag!='N': print "Error: Wrong Flag" self.cmdloop() elif len(args)==3: #case get <filename> <encflag = E> <password> filename, encflag, password = line.split(" ") if len(password)!=8: print "Password is short <8 Characters>" self.cmdloop() if encflag!='E': print "Wrong Flag" self.cmdloop() else: print "Wrong Number of Arguments" self.cmdloop() Common.send_msg(self.clientsocket,"get") Common.send_msg(self.clientsocket, filename) status = Common.recv_msg(self.clientsocket) if status=="OK": data = Common.recv_msg(self.clientsocket) fhash = Common.recv_msg(self.clientsocket) with open('tmp_client/'+filename+".enc","w") as f: f.write(data) with open('tmp_client/'+filename+".sha256","w") as f: f.write(fhash) fname = 'tmp_client/'+filename+".enc" if encflag=='E': #Client assumes the file was encrypted. if not Common.decrypt_file(password, fname): #File was not encrypted to begin with!! print ("Error: decryption of %s failed, was file encrypted?" %filename) os.remove('tmp_client/'+filename+".sha256") # sha of file os.remove('tmp_client/'+filename+".enc") #enc file else: #File decrypted check hash filehash = Common.gen_hash('tmp_client/'+filename) if fhash==filehash: print "retrieval of %s complete" %filename else: print ("Error: Computed hash of %s does not match " "retrieved hash" %filename) os.remove('tmp_client/'+filename) #Irrespecive of Match or not delete the hashed file. os.remove('tmp_client/'+filename+".sha256") os.remove('tmp_client/'+filename+".enc") else: #Client assumes no encryption was applied filehash = Common.gen_hash('tmp_client/'+filename+".enc") if fhash==filehash: print "retrieval of %s complete "%filename else: print ("Error: Computed hash of %s does not match " "retrieved hash" %filename) with open('tmp_client/'+filename,"w") as f: f.write(data) os.remove('tmp_client/'+filename+".sha256") os.remove('tmp_client/'+filename+".enc") else: #Server Error Occured. print status