def set_key_pair_from_string(self): # 3.1.3 questions = [ { 'type': 'input', 'name': 'private_key_store', 'message': 'Input private key store json:', }, { 'type': 'password', 'message': 'Enter the password to decrypt private key:', 'name': 'password', 'when': lambda answer: answer['private_key_store'] != '', }, ] new_password = [ { 'type': 'list', 'name': 'new_pwd', 'message': 'Do you want set a new password?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower(), } ] save_password = [ { 'type': 'list', 'name': 'save_pwd', 'message': 'Do you want to save password?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower(), } ] crypto = CryptoUtility(self.key_path) answer = prompt(questions) if answer['private_key_store'] != '': print('Setting key pair...') try: crypto.password = answer['password'] if not crypto.set_private_key(answer['private_key_store']): self.configure_key_pair() answer = prompt(new_password) if answer['new_pwd'] == 'yes': crypto.password = self._set_password() answer = prompt(save_password) if answer['save_pwd'] == 'yes': crypto.export_password_hash_to_file() else: crypto.delete_password_hash_file() print(crypto.export_private_key_to_file()) print(crypto.export_public_key_to_file()) print('Key pair successfully generated!') except Exception as e: print(e) self.configure_key_pair()
def generate_key_pair(self): # 3.1.1 questions = [{ 'type': 'list', 'name': 'regenerate', 'message': 'Do you want to regenerate the key pair?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower() }, { 'type': 'list', 'name': 'save_pwd', 'message': 'Do you want save password?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower(), 'when': lambda answer: answer['regenerate'] == 'yes' }] crypto = CryptoUtility() answer = prompt(questions, style=self.style) if answer['regenerate'] == 'yes': crypto.generate_key_pair() print('Generating key pair...') crypto.password = self._set_password() if answer['save_pwd'] == 'yes': print(crypto.export_password_hash_to_file()) else: crypto.delete_password_hash_file() print(crypto.export_private_key_to_file()) print(crypto.export_public_key_to_file()) print('Key pair successfully generated!\n') self._show_public_key() self.configure_key_pair() else: self.configure_key_pair()
def decrypt(self): # 2 questions = [ { 'type': 'input', 'name': 'cipher_text', 'message': 'Input encrypted cipher text:', } ] input_password = [ { 'type': 'password', 'name': 'password', 'message': 'Enter the password of private key to decrypt:', } ] answer = prompt(questions) crypto = CryptoUtility(self.key_path) if not crypto.password: input_pwd = prompt(input_password) crypto.password = input_pwd['password'] print(f' Decrypting...', end="\r") crypto.import_private_key_from_file() password = crypto.decrypt_text(answer['cipher_text']) qprint(f'Your password is:', style='#06c8ff', flush=True) qprint(f'\n{password}\n', style='bold #06c8ff')
def decrypt(self): # 2 questions = [{ 'type': 'input', 'name': 'cipher_text', 'message': 'Input encrypted cipher text:', }] input_password = [{ 'type': 'password', 'name': 'password', 'message': 'Enter the password to decrypt' }] answer = prompt(questions, style=self.style) crypto = CryptoUtility() if not crypto.password: input_pwd = prompt(input_password, style=self.style) crypto.password = input_pwd['password'] crypto.import_private_key_from_file() password = crypto.decrypt_text(answer['cipher_text']) print(f'Your password is: {password}') print() self.main_menu()
def save_private_key_password(self): # 3.1.5 input_password = [ { 'type': 'password', 'name': 'password', 'message': 'Enter the password to decrypt private key:', } ] crypto = CryptoUtility(self.key_path) if not crypto.password: input_pwd = prompt(input_password) crypto.password = input_pwd['password'] if crypto.import_private_key_from_file(): crypto.export_password_hash_to_file() print('Saved password successfully!') else: print('Wrong Password!') else: print('Password already saved.') self.configure_key_pair()