def test_can_generate_two_key_encrypt_and_decrypt(self): e = Encrypter() e.save_key('myprivatekey.pem') e.save_public_key('mypublickey.pem') public_key = e.read_key('mypublickey.pem') private_key = e.read_key('myprivatekey.pem') message = 'un texte' ciphertext = e.encrypt(message, public_key) decripted = e.decrypt(ciphertext, private_key) self.assertEqual(decripted, message)
class PasswordManager(object): ''' Simple CLI Password Manager Tool developed by python''' PASS_FILE = '{home}/.pass-manager.db'.format(home=os.environ['HOME']) def __init__(self): ''' if not db file, create new db file ''' # set master password self.encrypter = Encrypter() if os.path.exists(self.PASS_FILE): try: with open(self.PASS_FILE, 'r') as f: file_content = pickle.load(f) # for compatibility if type(file_content) in (dict, defaultdict): # old version self.passwds = file_content elif type(file_content) == str: # new version decrypted_file_content = \ self.encrypter.decrypt(file_content) self.passwds = json.loads(decrypted_file_content) else: raise Exception('Unknown error...') except Exception as e: print 'failed loading db file... ', e os.remove(self.PASS_FILE) raise Exception('try it again.') master = self.passwds['master'] else: self.passwds = {} master = getpass.getpass(\ 'This is your first time. Please input your master password => ') master = hashlib.sha256(master).hexdigest() self.passwds['master'] = master self._save_db() self.sha = hashlib.sha256(master) # self.sha = master_sha def _save_db(self): ''' save or update db file ''' try: with open(self.PASS_FILE, 'w') as f: json_passwds = json.dumps(self.passwds) encrypted_file_content = self.encrypter.encrypt(json_passwds) pickle.dump(encrypted_file_content, f) except Exception as e: raise Exception('failed saving db file :{e}'.format(e=e)) def setpb_data(self, data): ''' save password into clipboard ''' p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE) p.communicate(data) def passwd_generator(self, salt): ''' generate new password using master password and salt ''' self.sha.update(salt) digest = self.sha.hexdigest()[-12:] return self.passwd_strengthen(digest) def passwd_strengthen(self, digest): ''' strengthen generated password ''' strengthened_digest = [] signs = ['#', '$', '-', '=', '?', '@', '[', ']', '_'] for c in digest: odd = randint(1, 10) % 2 == 1 if c.isalpha() and odd: c = c.upper() elif c.isdigit() and not odd: sign = choice(signs) c = sign strengthened_digest.append(c) return ''.join(strengthened_digest) def save_passwd(self, salt, passwd): ''' save generated and strengthened password with salt as key ''' if self.passwds.has_key(salt): to_overwrite = "" while to_overwrite not in ['y', 'n']: to_overwrite = raw_input('Overwrite? (y or n)') if to_overwrite == 'n': return False self.passwds[salt] = passwd self._save_db() return True def confirm_master(self): ''' do auth by master password ''' valid_master = getpass.getpass(\ 'Please input master password => ') if valid_master == self.passwds['master']: # for secure, not save raw master password master = hashlib.sha256(self.passwds['master']).hexdigest() self.passwds['master'] = master self._save_db() valid_master = hashlib.sha256(valid_master).hexdigest() if valid_master == self.passwds['master']: return True else: return False def show_passwds(self): ''' show all passwords ''' valid_master = self.confirm_master() if valid_master: if len(self.passwds) == 1: print 'There is no password...' return for k, v in self.passwds.iteritems(): if k == 'master': continue print '{k}: {v}'.format(k=k, v=v) else: print 'Wrong password...' def create_passwd(self): ''' create new password ''' salt = raw_input(\ 'Input a keyword for password (like domain, url) => ') while salt is 'master': print 'Sorry, you cannot use "master"' salt = raw_input('Try it again => ') passwd = self.passwd_generator(salt) is_updated = self.save_passwd(salt, passwd) passwd = passwd if is_updated else self.passwds[salt] # print 'パスワードはこちらです\n\n{0}'.format(passwd) print 'Password saved int your clipboard.' self.setpb_data(passwd) def load_passwd(self): ''' load saved password ''' valid_master = self.confirm_master() if valid_master: salt = raw_input(\ 'Input keyword you want to load => ') if self.passwds.has_key(salt): print 'Password saved int your clipboard.' # print self.passwds[salt] self.setpb_data(self.passwds[salt]) else: print 'Such keyword({0}) is not setted...'\ .format(salt) else: print 'Wrong password...' def update_master(self): ''' update master password ''' valid_master = self.confirm_master() if valid_master: master = getpass.getpass('Input new master password => ') self.passwds['master'] = master self._save_db() print 'Success!' else: print 'Wrong password...' def delete_passwd(self): '''作成済みのパスワードを削除する ''' salt = raw_input('Input keyword you want to delete => ') if self.passwds.has_key(salt): print 'Deleted {0}.'.format(self.passwds[salt]) else: print 'Such keyword({0}) is not setted...'.format(salt) return self.passwds.pop(salt) self._save_db()
mode = 'CTR' break elif mode == '4' or mode == 'CBC': mode = 'CBC' break print(errorMsg) print(modeSetMsg + mode + '.') ciphertext = encrypter.encrypt(message, mode) print(cipherReadyMsg + str(ciphertext.replace(' ', ''))) print(decryptMsg) while True: decrypt = input() if decrypt == 'y': decryptedCipher = encrypter.decrypt(ciphertext, mode) print(decryptionDoneMsg + str(decryptedCipher)) break elif decrypt == 'n': break print(errorMsg) print(loopMsg) while True: loop = input() if loop == 'y': break elif loop == 'n': stop = True break print(errorMsg)
input( "\nElija una opción => 1:Encriptar - 2:Desencriptar - 3:Finalizar :\t" )) # Opción para enciptar una frase if option == 1: print('\nECRIPTADO') speed = int(input("Indique la frecuencia de encriptado: ")) # cambiamos la frecuencia del encriptador encripter.spinner(speed) chars = input("Escriba su frase: ") # encriptamos la frase y devolvemos el hash encripter.encrypt(chars) option = 1 # Opción para desenciptar un hash if option == 2: print('\nDESENCRIPTADO') speed = int(input("Indique la frecuencia de encriptado: ")) # cambiamos la frecuencia del encriptador encripter.spinner(speed) hash_encrypt = input("Ingrese su hash: ") # desencriptamos la frase a partir de un hash encripter.decrypt(hash_encrypt) option = 2 # Opción para terminar la ejecución del programa if option == 3: print('\n------ Fin del programa -------\n') option = 3