def decrypt_file(self, key, in_file, output, chunksize=64 * 8192): ''' Summary: Handles file decryption with AES 256 and returns related information Arguments: key string, to be used for decryption in_file string, target file to be decrypted chunksize int, size of chunks to be decrypted Returns: dict ''' start_run_time = timer() start_date_time = str(datetime.now()) if in_file.split('.')[-1] != 'crypt': return {'result': 'fail', 'reason': 'File is not a .crypt file'} with open(in_file, 'rb') as encrypted_file: original_size = struct.unpack( '<Q', encrypted_file.read(struct.calcsize('Q')))[0] initialisation_vector = encrypted_file.read(16) aes = AES.new(key, AES.MODE_CBC, initialisation_vector) chunks_decrypted = 0 with open(output, 'wb') as plaintext: try: while True: encrypted_chunk = encrypted_file.read(chunksize) chunks_decrypted += 1 if len(encrypted_chunk) == 0: break plaintext.write(aes.decrypt(encrypted_chunk)) plaintext.truncate(original_size) except: return {'result': 'fail', 'reason': sys.exc_info()[0]} end_run_time = timer() return { 'result': 'success', 'data': { 'key': key, 'iv': initialisation_vector.encode('hex'), 'encrypted_file_size': os.path.getsize(in_file), 'plaintext_file_size': os.path.getsize(output), 'chunks_decrypted': chunks_decrypted, 'output_file': output }, 'start_time': start_date_time, 'end_time': str(datetime.now()), 'run_time': runtime(start_run_time, end_run_time), 'user': getpass.getuser(), 'hostname': socket.gethostname() }
def _task_end_signals(start_data): end_data = { 'end_time': timer(), 'end_date_time': str(datetime.now()), 'run_time': timer() - start_data['start_time'] } end_data.update(start_data) return end_data
def _task_start_signals(module_order): create_task_start_temp_file() create_task_links_temp_file() return { 'results': { 'modules': [] }, 'start_time': timer(), 'start_date_time': str(datetime.now()), 'start_user': getpass.getuser(), 'start_hostname': socket.gethostname() }
def _module_result(self, module_result): result = module_result['result'] store = module_result['store'] make_it_look_important() result_dict = { 'result': result, 'store': store, 'end_time': timer(), 'run_time': timer() - self.module_start_data['start_time'], 'end_date_time': str(datetime.now()) } make_it_look_important() result_dict.update(self.module_start_data) result_dict.update({'module': self.module}) result_dict.update({'uuid': str(uuid.uuid4())}) make_it_look_important() if result == 'success': result_dict.update({'next_module': self.next_module}) module_success(module_result['delay']) elif result == 'branch': result_dict.update( {'next_module': module_result['branching_module']}) module_branch(module_result['branching_module'], module_result['delay']) elif result == 'end': result_dict.update({'next_module': ''}) module_end() elif result == 'fail': module_fail() result_dict.update({'next_module': ''}) elif result == 'retry': module_retry(module_result['delay']) result_dict.update({'next_module': self.module}) return result_dict
def encrypt_file(self, key, in_file, chunksize=64 * 8192): ''' Summary: Handles file encryption with AES 256 and returns related information Arguments: key string, to be used for encryption in_file string, target file to be encrypted chunksize int, size of chunks to be encrypted Returns: dict ''' start_run_time = timer() start_date_time = str(datetime.now()) initialisation_vector = ''.join( chr(random.randint(0, 0xFF)) for i in range(16)) if not key: key = self.generate_aes_encryption_key() aes = AES.new(key, AES.MODE_CBC, initialisation_vector) chunks_encrypted = 0 out_file = in_file + '.crypt' with open(in_file, 'rb') as plaintext: with open(out_file, 'wb') as encrypted_file: try: encrypted_file.write( struct.pack('<Q', os.path.getsize(in_file))) encrypted_file.write(initialisation_vector) while True: plaintext_chunk = plaintext.read(chunksize) chunks_encrypted += 1 if len(plaintext_chunk) == 0: break elif len(plaintext_chunk) % 16 != 0: plaintext_chunk += ' ' * ( 16 - len(plaintext_chunk) % 16) encrypted_file.write(aes.encrypt(plaintext_chunk)) except: return {'result': 'fail', 'reason': sys.exc_info()[0]} end_run_time = timer() return { 'result': 'success', 'data': { 'key': key, 'iv': initialisation_vector.encode('hex'), 'encrypted_file_size': os.path.getsize(out_file), 'plaintext_file_size': os.path.getsize(in_file), 'chunks_encrypted': chunks_encrypted, 'output_file': out_file }, 'start_time': start_date_time, 'end_time': str(datetime.now()), 'run_time': runtime(start_run_time, end_run_time), 'user': getpass.getuser(), 'hostname': socket.gethostname() }
def _module_start_signals(self, module): module_start_header(module) return {'start_time': timer(), 'start_date_time': str(datetime.now())}