def decrypt_message(message): config = cherrypy.request.app.config['filelocker'] messageBody = "" try: path = os.path.join(config['vault'], "m" + str(message.id)) bodyfile = open(path, 'rb') salt = bodyfile.read(16) decrypter = Encryption.new_decrypter(message.encryption_key, salt) endOfFile = False readData = bodyfile.read(1024 * 8) data = decrypter.decrypt(readData) #If the data is less than one block long, just process it and send it out if len(data) < (1024 * 8): padding = int(str(data[-1:]), 16) #A 0 represents that the file had a multiple of 16 bytes, and 16 bytes of padding were added if padding == 0: padding = 16 endOfFile = True messageBody += data[:len(data) - padding] else: #For multiblock files while True: if endOfFile: break next_data = decrypter.decrypt(bodyfile.read(1024 * 8)) if (next_data is not None and next_data != "") and not len(next_data) < (1024 * 8): yData = data data = next_data messageBody += yData #This prevents padding going across block boundaries by aggregating the last two blocks and processing #as a whole if the next block is less than a full block (signifying end of file) else: data = data + next_data padding = int(str(data[-1:]), 16) #A 0 represents that the file had a multiple of 16 bytes, and 16 bytes of padding were added if padding == 0: padding = 16 endOfFile = True messageBody += data[:len(data) - padding] return messageBody except Exception, e: raise Exception("Couldn't decrypt message body: %s" % str(e))
def decrypt_message(message): config = cherrypy.request.app.config["filelocker"] messageBody = "" try: path = os.path.join(config["vault"], "m" + str(message.id)) bodyfile = open(path, "rb") salt = bodyfile.read(16) decrypter = Encryption.new_decrypter(message.encryption_key, salt) endOfFile = False readData = bodyfile.read(1024 * 8) data = decrypter.decrypt(readData) # If the data is less than one block long, just process it and send it out if len(data) < (1024 * 8): padding = int(str(data[-1:]), 16) # A 0 represents that the file had a multiple of 16 bytes, and 16 bytes of padding were added if padding == 0: padding = 16 endOfFile = True messageBody += data[: len(data) - padding] else: # For multiblock files while True: if endOfFile: break next_data = decrypter.decrypt(bodyfile.read(1024 * 8)) if (next_data is not None and next_data != "") and not len(next_data) < (1024 * 8): yData = data data = next_data messageBody += yData # This prevents padding going across block boundaries by aggregating the last two blocks and processing # as a whole if the next block is less than a full block (signifying end of file) else: data = data + next_data padding = int(str(data[-1:]), 16) # A 0 represents that the file had a multiple of 16 bytes, and 16 bytes of padding were added if padding == 0: padding = 16 endOfFile = True messageBody += data[: len(data) - padding] return messageBody except Exception, e: raise Exception("Couldn't decrypt message body: %s" % str(e))
ext = "" i = flFile.name.rfind('.') if i != -1: ext = flFile.name[i:].lower() content_type = mimetypes.types_map.get(ext, "text/plain") response.headers['Content-Type'] = content_type if disposition is not None: cd = '%s; filename="%s"' % (disposition, flFile.name) response.headers["Content-Disposition"] = cd # Set Content-Length and use an iterable (file object) # this way CP won't load the whole file in memory c_len = st.st_size bodyfile = open(path, 'rb') salt = bodyfile.read(16) decrypter = Encryption.new_decrypter(flFile.encryption_key, salt) try: response.body = self.enc_file_generator(user, decrypter, bodyfile, flFile.id, publicShareId) return response.body except cherrypy.HTTPError, he: raise he def enc_file_generator(self, user, decrypter, dFile, fileId=None, publicShareId=None): endOfFile = False readData = dFile.read(1024 * 8)
ext = "" i = flFile.name.rfind('.') if i != -1: ext = flFile.name[i:].lower() content_type = mimetypes.types_map.get(ext, "text/plain") response.headers['Content-Type'] = content_type if disposition is not None: cd = '%s; filename="%s"' % (disposition, flFile.name) response.headers["Content-Disposition"] = cd # Set Content-Length and use an iterable (file object) # this way CP won't load the whole file in memory c_len = st.st_size bodyfile = open(path, 'rb') salt = bodyfile.read(16) decrypter = Encryption.new_decrypter(flFile.encryption_key, salt) try: response.body = self.enc_file_generator(user, decrypter, bodyfile, flFile.id, publicShareId) return response.body except cherrypy.HTTPError, he: raise he def enc_file_generator(self, user, decrypter, dFile, fileId=None, publicShareId=None): endOfFile = False readData = dFile.read(1024*8) data = decrypter.decrypt(readData) #If the data is less than one block long, just process it and send it out #try: if len(data) < (1024*8): padding = int(str(data[-1:]),16) #A 0 represents that the file had a multiple of 16 bytes, and 16 bytes of padding were added