Advertisement
Guest User

phonebook cs hw you racist cunts

a guest
Mar 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.89 KB | None | 0 0
  1. from base64 import b64encode, b64decode
  2. import os
  3. import sys
  4. from getpass import getuser #for salt
  5.  
  6. class InvalidKey(Exception):
  7.     pass
  8.  
  9. def encode(key, salt, clear):
  10.     """ Encodes text based on key for security
  11. Creates new variable result, and for each letter in key,
  12. do ord(letter) and add it to result. Once completed,
  13. divide and round the number (/2 and rounded again) by the first half of the number
  14. (for example 1100 would be divided by 11 and rounded"""
  15.  
  16.     #salt will be used to defend against rainbow tables even though the encoding isnt safe
  17.    
  18.     result = 0
  19.     for letter in key:
  20.         result+=(ord(letter)) #now result will be something
  21.  
  22.     #get n amount of characters to get, and set it to divider variable
  23.     divider = int(str(result)[:round(len(str(result)) / 2)])
  24.     keyresult = round(round(result / 2) / divider)
  25.     if keyresult <= 0:
  26.         raise InvalidKey('Key cannot be used, try another')
  27.  
  28.     #prepare clear for encoding
  29.     clear = bytes(clear, 'utf8')
  30.  
  31.     for i in range(1,keyresult + 1):
  32.         clear = b64encode(clear)
  33.  
  34.     return salt + clear.decode('utf8')
  35.  
  36. def decode(key, salt, clear):
  37.     clear = clear.replace(salt, '') #remove salt
  38.     clear = bytes(clear, 'utf8')
  39.  
  40.     result = 0
  41.     for letter in key:
  42.         result+=(ord(letter)) #now result will be something
  43.  
  44.     #get n amount of characters to get, and set it to divider variable
  45.     divider = int(str(result)[:round(len(str(result)) / 2)])
  46.     keyresult = round(round(result / 2) / divider)
  47.     if keyresult <= 0:
  48.         raise InvalidKey('Key cannot be used, try another')
  49.  
  50.     for i in range(1, keyresult + 1):
  51.         clear = b64decode(clear)
  52.  
  53.     try:
  54.         b64decode(clear)
  55.         return InvalidKey('Key is not valid')
  56.     except:
  57.         pass
  58.  
  59.     clear = clear.decode('utf8')
  60.     return clear
  61.  
  62.  
  63. """phonebook format:
  64. firstname,lastname,phonenumber,address"""
  65.  
  66. def menu():
  67.     print("""
  68. -------------------------------
  69.           PHONEBOOK
  70. -------------------------------
  71. [1] List all numbers
  72. [2] Add a number
  73. [3] Remove a number
  74. [4] Edit a number
  75. [5] Exit""")
  76.     while True:
  77.         inp = input("> ")
  78.         #validate
  79.         if inp == '1':
  80.             with open('phone.book', 'r') as f:
  81.                 c = f.read()
  82.             if len(c) == 0:
  83.                 print("[+] No phone numbers")
  84.                 continue
  85.             decoded = decode(pw, salt, c)
  86.             decoded = decoded.split(':')
  87.             for info in range(len(decoded)):
  88.                 decoded[info] = decoded[info].split(',')
  89.  
  90.             for number in decoded:
  91.                 print('---------------')
  92.                 print('Firstname: {}'.format(number[0]))
  93.                 print('Surname: {}'.format(number[1]))
  94.                 print('Number: {}'.format(number[2]))
  95.                 print('Address: {}'.format(number[3]))
  96.  
  97.             print('---------------')
  98.         elif inp == '2':
  99.             firstname = input("Firstname: ").capitalize()
  100.             lastname = input("Surname: ").capitalize()
  101.             number = input("Number: ")
  102.             address = input("Address: ")
  103.             fformat = firstname + ',' + lastname + ',' + number + ',' + address + '\n'
  104.             with open('phone.book', 'a') as f:
  105.                 f.write(encode(pw, salt, fformat))
  106.             print("Added {}'s number".format(firstname))
  107.         elif inp == '3':
  108.             with open('phone.book', 'r') as f:
  109.                 c = f.read()
  110.             c = decode(pw, salt, c)
  111.             c = c.split(':')
  112.             firstname = input("Firstname: ")
  113.             print("Deleting..\nThis could take long if you have a lot of numbers")
  114.             result = []
  115.             for data in c:
  116.                 if data.startswith(firstname):
  117.                     continue
  118.                 else:
  119.                     result.append(data)
  120.             result = encode(pw, salt, ':'.join(result))
  121.             with open('phone.book', 'w') as f:
  122.                 f.write(result)
  123.             print("Deleted contact.")
  124.         elif inp == '4':
  125.             with open('phone.book', 'r') as f:
  126.                 c = f.read()
  127.             c = decode(pw, salt, c)
  128.             c = c.split(':')
  129.             firstname = input("Firstname: ")
  130.             edit = None
  131.             for data in c:
  132.                 if data.startswith(firstname):
  133.                     edit = data
  134.                     break
  135.             if edit == None:
  136.                 print("Couldn't find anyone called {} in your phonebook.".format(firstname))
  137.                 continue
  138.             data = data.split(',')
  139.             print("\nType FINISH to finish")
  140.             print("Syntax: type,new value")
  141.             print("Example: address,7 Something Road")
  142.             while True:
  143.                 print('--------------------')
  144.                 print('Firstname: {}'.format(data[0]))
  145.                 print('Surname: {}'.format(data[1]))
  146.                 print('Number: {}'.format(data[2]))
  147.                 print('Address: {}'.format(data[3]))
  148.                 print('--------------------')
  149.                 etype = input("Edit: ")
  150.                 if etype == 'FINISH':
  151.                     break
  152.                 else:
  153.                     etype = etype.split(',')
  154.                 if etype[0]=='firstname':
  155.                     data[0] = etype[1]
  156.                 elif etype[0]=='surname':
  157.                     data[1] = etype[1]
  158.                 elif etype[0]=='number':
  159.                     data[2] = etype[1]
  160.                 elif etype[0]=='address':
  161.                     data[3] = etype[1]
  162.                 else:
  163.                     print("Invalid syntax")
  164.         elif inp == '5':
  165.             sys.exit()
  166.                
  167.  
  168. def createfile(file):
  169.     """ Creates file by opening with mode w+ and closing"""
  170.     open(file, 'w+').close()
  171.  
  172. createfile('phone.book')
  173. print("Password should be 5 or less characters")
  174. pw = input("Enter password: ")
  175. salt = getuser()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement