Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from base64 import b64encode, b64decode
- import os
- import sys
- from getpass import getuser #for salt
- class InvalidKey(Exception):
- pass
- def encode(key, salt, clear):
- """ Encodes text based on key for security
- Creates new variable result, and for each letter in key,
- do ord(letter) and add it to result. Once completed,
- divide and round the number (/2 and rounded again) by the first half of the number
- (for example 1100 would be divided by 11 and rounded"""
- #salt will be used to defend against rainbow tables even though the encoding isnt safe
- result = 0
- for letter in key:
- result+=(ord(letter)) #now result will be something
- #get n amount of characters to get, and set it to divider variable
- divider = int(str(result)[:round(len(str(result)) / 2)])
- keyresult = round(round(result / 2) / divider)
- if keyresult <= 0:
- raise InvalidKey('Key cannot be used, try another')
- #prepare clear for encoding
- clear = bytes(clear, 'utf8')
- for i in range(1,keyresult + 1):
- clear = b64encode(clear)
- return salt + clear.decode('utf8')
- def decode(key, salt, clear):
- clear = clear.replace(salt, '') #remove salt
- clear = bytes(clear, 'utf8')
- result = 0
- for letter in key:
- result+=(ord(letter)) #now result will be something
- #get n amount of characters to get, and set it to divider variable
- divider = int(str(result)[:round(len(str(result)) / 2)])
- keyresult = round(round(result / 2) / divider)
- if keyresult <= 0:
- raise InvalidKey('Key cannot be used, try another')
- for i in range(1, keyresult + 1):
- clear = b64decode(clear)
- try:
- b64decode(clear)
- return InvalidKey('Key is not valid')
- except:
- pass
- clear = clear.decode('utf8')
- return clear
- """phonebook format:
- firstname,lastname,phonenumber,address"""
- def menu():
- print("""
- -------------------------------
- PHONEBOOK
- -------------------------------
- [1] List all numbers
- [2] Add a number
- [3] Remove a number
- [4] Edit a number
- [5] Exit""")
- while True:
- inp = input("> ")
- #validate
- if inp == '1':
- with open('phone.book', 'r') as f:
- c = f.read()
- if len(c) == 0:
- print("[+] No phone numbers")
- continue
- decoded = decode(pw, salt, c)
- decoded = decoded.split(':')
- for info in range(len(decoded)):
- decoded[info] = decoded[info].split(',')
- for number in decoded:
- print('---------------')
- print('Firstname: {}'.format(number[0]))
- print('Surname: {}'.format(number[1]))
- print('Number: {}'.format(number[2]))
- print('Address: {}'.format(number[3]))
- print('---------------')
- elif inp == '2':
- firstname = input("Firstname: ").capitalize()
- lastname = input("Surname: ").capitalize()
- number = input("Number: ")
- address = input("Address: ")
- fformat = firstname + ',' + lastname + ',' + number + ',' + address + '\n'
- with open('phone.book', 'a') as f:
- f.write(encode(pw, salt, fformat))
- print("Added {}'s number".format(firstname))
- elif inp == '3':
- with open('phone.book', 'r') as f:
- c = f.read()
- c = decode(pw, salt, c)
- c = c.split(':')
- firstname = input("Firstname: ")
- print("Deleting..\nThis could take long if you have a lot of numbers")
- result = []
- for data in c:
- if data.startswith(firstname):
- continue
- else:
- result.append(data)
- result = encode(pw, salt, ':'.join(result))
- with open('phone.book', 'w') as f:
- f.write(result)
- print("Deleted contact.")
- elif inp == '4':
- with open('phone.book', 'r') as f:
- c = f.read()
- c = decode(pw, salt, c)
- c = c.split(':')
- firstname = input("Firstname: ")
- edit = None
- for data in c:
- if data.startswith(firstname):
- edit = data
- break
- if edit == None:
- print("Couldn't find anyone called {} in your phonebook.".format(firstname))
- continue
- data = data.split(',')
- print("\nType FINISH to finish")
- print("Syntax: type,new value")
- print("Example: address,7 Something Road")
- while True:
- print('--------------------')
- print('Firstname: {}'.format(data[0]))
- print('Surname: {}'.format(data[1]))
- print('Number: {}'.format(data[2]))
- print('Address: {}'.format(data[3]))
- print('--------------------')
- etype = input("Edit: ")
- if etype == 'FINISH':
- break
- else:
- etype = etype.split(',')
- if etype[0]=='firstname':
- data[0] = etype[1]
- elif etype[0]=='surname':
- data[1] = etype[1]
- elif etype[0]=='number':
- data[2] = etype[1]
- elif etype[0]=='address':
- data[3] = etype[1]
- else:
- print("Invalid syntax")
- elif inp == '5':
- sys.exit()
- def createfile(file):
- """ Creates file by opening with mode w+ and closing"""
- open(file, 'w+').close()
- createfile('phone.book')
- print("Password should be 5 or less characters")
- pw = input("Enter password: ")
- salt = getuser()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement