def zip_folder_then_encrypt(self, password, should_remove): try: self.validate_entries(password) f_out_name = Path(str(self.path) + '.kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name.name + ' already exists') self.js_log_msg('Zipping...') zp = zip_dir(self.path) # Creates a temporary zip file target_size = os.stat(zp).st_size if target_size == 0: raise Exception(self.path.name + ' is empty') self.progress.set_total_size(target_size) self.js_log_msg('Encrypting...') key, salt = derive_key(password, None) lib.encryptor.encrypt(key, salt, zp, self.buffer_size) os.remove(zp) if should_remove: shutil.rmtree(self.path) self.path = Path('') self.js_reset_encrypt_form() self.js_log_msg('Done') except Exception as err: self.js_clear_logs() self.js_log_error(err.args[0]) finally: self.progress.reset() self.js_enable_form()
def encrypt_file(self, password, should_remove): try: self.validate_entries(password) if file_ext(self.path) == 'kpk': raise Exception('can not encrypt ' + self.path.name) f_out_name = replace_file_ext(self.path, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name.name + ' already exists') target_size = os.stat(self.path).st_size if target_size == 0: raise Exception(self.path.name + ' is empty') self.progress.set_total_size(target_size) self.js_log_msg('Encrypting...') key, salt = derive_key(password, None) lib.encryptor.encrypt(key, salt, self.path, self.buffer_size) if should_remove: os.remove(self.path) self.path = Path('') self.js_reset_encrypt_form() self.js_log_msg('Done') except Exception as err: self.js_clear_logs() self.js_log_error(err.args[0]) finally: self.progress.reset() self.js_enable_form()
def encrypt_folder(self, password, should_remove): try: self.validate_entries(password) self.js_log_msg('Looking for files in the directory...') ff = list_files(self.path, ENCRYPT_MODE) # List of files in the directory if len(ff) == 0: raise Exception(self.path.name + ' is empty') target_size = calc_total_size(ff) if target_size == 0: raise Exception(self.path.name + ' is empty') self.progress.set_total_size(target_size) self.js_log_msg('Encrypting...') key, salt = derive_key(password, None) for f in ff: f_out_name = replace_file_ext(f, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name.name + ' already exists') lib.encryptor.encrypt(key, salt, f, self.buffer_size) if should_remove: os.remove(f) self.path = Path('') self.js_reset_encrypt_form() self.js_log_msg('Done') except Exception as err: self.js_clear_logs() self.js_log_error(err.args[0]) finally: self.progress.reset() self.js_enable_form()
def decrypt(password: str, f_in_path: Path, buffer_size: int) -> (Path, str): header = _read_header(f_in_path) key, _ = derive_key(password, header['salt']) decryptor = Cipher( algorithms.AES(key), modes.CBC(header['iv']), backend=default_backend() ).decryptor() f_out_ext = _unpad_ext(decryptor.update(header['cipher_ext'])) f_out_path = replace_file_ext(f_in_path, f_out_ext) pipeline = new_pipeline(buffer_size) with open(f_in_path, 'rb') as fd_in: fd_in.seek(48) # Skip the header with open(f_out_path, 'wb') as fd_out: pipeline( fd_in, decryptor.update, _unpad_bytes, fd_out ) fd_out.write(decryptor.finalize()) return f_out_path, f_out_ext
def __init__(self, password, f_in_path): self.__progress = Progress.get_instance() self.__fd_in = open(f_in_path, 'rb') meta = self.__read_meta() key, _ = derive_key(password, meta['salt']) self.__decryptor = self.__aes_decryptor(key, meta['iv']) self.__f_out_ext = self.__decrypt_ext(meta['c_ext']) self.__f_out_path = replace_file_ext(f_in_path, self.__f_out_ext)
def encrypt_file(target_path: Path, should_remove: bool, buffer_size: int): if file_ext(target_path) == 'kpk': raise Exception('can not encrypt ' + str(target_path)) f_out_name = replace_file_ext(target_path, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(str(f_out_name) + ' already exists') target_size = os.stat(target_path).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') password = get_password(ENCRYPT_MODE) print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) lib.encryptor.encrypt(key, salt, target_path, buffer_size) if should_remove: os.remove(target_path)
def zip_dir_then_encrypt(target_path: Path, should_remove: bool, buffer_size: int): f_out_name = str(target_path) + '.kpk' if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name + ' already exists') password = get_password(ENCRYPT_MODE) print('\nZipping...') zp = zip_dir(target_path) # Creates a temporary zip file target_size = os.stat(zp).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) lib.encryptor.encrypt(key, salt, zp, buffer_size) os.remove(zp) if should_remove: shutil.rmtree(target_path)
def encrypt_dir(target_path: Path, should_remove: bool, buffer_size: int): password = get_password(ENCRYPT_MODE) print('\nLooking for files in the directory...') ff = list_files(target_path, ENCRYPT_MODE) # List of files in the directory if len(ff) == 0: raise Exception(str(target_path) + ' is empty') target_size = calc_total_size(ff) if target_size == 0: raise Exception(str(target_path) + ' is empty') print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) for f in ff: f_out_name = replace_file_ext(f, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(str(f_out_name) + ' already exists') lib.encryptor.encrypt(key, salt, f, buffer_size) if should_remove: os.remove(f)
def execute(argv): if len(argv) == 2 or argv[2] == '-h' or argv[2] == '--help': print_help_encrypt() return should_remove = True if has_remove_flag(argv[2:]) else False should_zip = True if has_zip_flag(argv[2:]) else False target_path = get_path(argv[2:]) if target_path == None: raise Exception('PATH is not specified') if not os.path.exists(target_path): raise Exception('can not find ' + str(target_path)) if os.path.isfile(target_path): if file_ext(target_path) == 'kpk': raise Exception('can not encrypt ' + str(target_path)) f_out_name = replace_file_ext(target_path, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name + ' already exists') target_size = os.stat(target_path).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') password = get_password(ENCRYPT_MODE) print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) encryptor = FileEncryptor(key, salt, target_path) encryptor.encrypt() if should_remove: os.remove(target_path) print('\r' + 20 * ' ' + '\r[■■■■■■■■■■] 100%\n' ) # TODO: This is a hack, should be fixed in lib/progress.py return if should_zip and os.path.isdir(target_path): f_out_name = str(target_path) + '.kpk' if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name + ' already exists') password = get_password(ENCRYPT_MODE) print('\nZipping...') zp = zip_dir(target_path) # Creates a temporary zip file target_size = os.stat(zp).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) encryptor = FileEncryptor(key, salt, zp) encryptor.encrypt() os.remove(zp) if should_remove: shutil.rmtree(target_path) return if os.path.isdir(target_path): password = get_password(ENCRYPT_MODE) print('\nLooking for files in the directory...') ff = list_files(target_path, ENCRYPT_MODE) # List of files in the directory if len(ff) == 0: raise Exception(str(target_path) + ' is empty') target_size = calc_total_size(ff) if target_size == 0: raise Exception(str(target_path) + ' is empty') print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) for f in ff: f_out_name = replace_file_ext(f, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name + ' already exists') encryptor = FileEncryptor(key, salt, f) encryptor.encrypt() if should_remove: os.remove(f) print('\r' + 20 * ' ' + '\r[■■■■■■■■■■] 100%\n' ) # TODO: This is a hack, should be fixed in lib/progress.py