Advertisement
Guest User

Untitled

a guest
Jan 27th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.89 KB | None | 0 0
  1. import time
  2. import random #random integers
  3. import base64 #encryption
  4. import tkinter #gui
  5. import getpass #hiding password input
  6. import re #character filter
  7. import os #file management
  8. import sys #file management
  9. import shutil #file management
  10.  
  11. #establishing variables
  12.  
  13. #account creation variables
  14. account_created = False;
  15. #security quesion creation variables
  16. security_question_creation=False;
  17. sq1_verified=True
  18. sq2_verified=True
  19. sq3_verified=True
  20. sq_verfication = set("1one2two3three")
  21. first_run=True
  22.  
  23. #sign-in variables
  24. verification_complete = False;
  25. verification_attempts = 6
  26. verification_delay = False;
  27. username_missmatch=False
  28.  
  29. #username changing variables
  30. username_change_complete = False;
  31.  
  32. #password changing variables
  33. old_attempts = 6
  34. old_delay = False;
  35. password_change_complete = False;
  36.  
  37. #password recovery variables
  38. password_recovery_complete = False;
  39. recovery_attempts=2
  40. x=random.randint(1,3)
  41.  
  42. #miscellaneous variables
  43. password_delay=3 #set to 30 for final version
  44. selection = ""
  45. username=""
  46.  
  47. #encryption
  48. def encrypt(message):
  49. key="LxsAEgwYRQIGRRAKEhdP"
  50. enc = []
  51. for i in range(len(message)):
  52. key_c = key[i % len(key)]
  53. enc_c = chr((ord(message[i]) + ord(key_c)) % 256)
  54. enc.append(enc_c)
  55. return base64.urlsafe_b64encode("".join(enc).encode()).decode()
  56.  
  57. #file management
  58. def read(file):
  59. f = open(file, "r")
  60. string=(f.read)()
  61. string=str(string)
  62. f.close()
  63. return string
  64.  
  65. def save(data, file):
  66. f= open(file,"w+")
  67. f.write(data)
  68. f.close()
  69.  
  70. def save_as_encrypted(data, file):
  71. f= open(file,"w+")
  72. f.write(encrypt(data))
  73. f.close()
  74.  
  75. #selection menu
  76. def selection1():
  77. global selection
  78. selection = int(1)
  79. selection_menu.destroy()
  80.  
  81. def selection2():
  82. global selection
  83. selection = int(2)
  84. selection_menu.destroy()
  85.  
  86. def selection3():
  87. global selection
  88. selection = int(3)
  89. selection_menu.destroy()
  90.  
  91. def selection4():
  92. global selection
  93. selection = int(4)
  94. selection_menu.destroy()
  95.  
  96. def selection5():
  97. global selection
  98. selection = int(5)
  99. selection_menu.destroy()
  100.  
  101. def exit():
  102. quit()
  103.  
  104. def createFolder(directory):
  105. try:
  106. if not os.path.exists(directory): #createFolder("./data/")
  107. os.makedirs(directory)
  108. except OSError:
  109. print ("Error: Creating directory. " + directory)
  110.  
  111. ################PROGRAM STARTS################
  112.  
  113. while(selection==""):
  114. selection_menu = tkinter.Tk()
  115. selection_menu.title("Selection")
  116.  
  117. label = tkinter.Label(selection_menu, text="Press a button to continue:\n", font="none 12 bold")
  118. button = tkinter.Button(selection_menu, text="Sign In", command=selection1)
  119. button2 = tkinter.Button(selection_menu, text="Create An Account", command=selection2)
  120. button3 = tkinter.Button(selection_menu, text="Change Username", command=selection3)
  121. button4 = tkinter.Button(selection_menu, text="Change Password", command=selection4)
  122. button5 = tkinter.Button(selection_menu, text="Account Recovery []", command=selection5)
  123. button6 = tkinter.Button(selection_menu, text="Exit", command=exit)
  124.  
  125. label.pack()
  126. button.pack()
  127. button2.pack()
  128. button3.pack()
  129. button4.pack()
  130. button5.pack()
  131. button6.pack()
  132. selection_menu.geometry("240x220")
  133. selection_menu.mainloop()
  134.  
  135. while(verification_complete==False):
  136. #sign-in process
  137. if(selection==1):
  138. if(username=="" or username_missmatch==True):
  139. username=input("Please enter your USERNAME: ")
  140. username_missmatch=False
  141. else:
  142. if(os.path.exists("./" + str(username))==True):
  143. password=read("./" + str(username) + "/password.txt")
  144. if(int(verification_attempts)==0):
  145. print("Too many unsuccessful attempts. Please try again later\n")
  146. time.sleep(3)
  147. input("\nPress [ENTER] to Exit")
  148. exit()
  149. else:
  150. if(int(verification_attempts)==3 and verification_delay==False):
  151. print("Too many unsuccessful attempts. Please try again in 30 seconds\n")
  152. verification_delay=True
  153. time.sleep(password_delay)
  154. else:
  155. passcheck=encrypt((getpass.getpass("Please enter your PASSWORD (NOTE: Your input will be hidden): ")))
  156. if passcheck==password:
  157. verification_complete = True
  158. else:
  159. verification_attempts=verification_attempts - 1
  160. print("Password incorrect. You have " + str(verification_attempts) + " attempts remaining\n")
  161. else:
  162. print("That username does not match. Please check you have typed it correctly or CREATE AN ACCOUNT if you havent already done so.\n")
  163. username_missmatch=True
  164.  
  165. else:
  166. #password creation
  167. if(selection==2):
  168. while(account_created==False):
  169. username=input("Please enter your desired username (NOTE: You may ONLY use characters a-z and it MUST be between 5 and 15 characters long): ")
  170. if(os.path.exists("./" + str(username) + "/")==False):
  171. if not re.match("^[a-z]*$", username):
  172. print("Your username may only contain letters a-z. Please try again.\n")
  173. time.sleep(1)
  174. else:
  175. if len(username) > 15 or len(username) < 5:
  176. print("Username Must be between 5 and 15 characters long. Please try again.\n")
  177. time.sleep(1)
  178. else:
  179. password=(getpass.getpass("\nPlease enter your desired password. It must contain AT LEAST 6 characters (NOTE: Your input will be hidden): "))
  180. if len(password)<6:
  181. print("Password must be AT LEAST 6 characters long. Please try again.\n")
  182. time.sleep(1)
  183. else:
  184. passcheck=(getpass.getpass("Please re-enter your desired password (NOTE: Your input will be hidden): "))
  185. if passcheck==password:
  186. createFolder("./" + str(username) + "/")
  187. account_created = True
  188. save_as_encrypted(password, "./" + str(username) + "/password.txt.")
  189. print("\n")
  190. while(security_question_creation==False):
  191. if(first_run==True or sq1_verified==False):
  192. sq1=input("Please enter your first security question: ")
  193. sq1a=input("Please type the answer for your first security question: ")
  194. if(first_run==True or sq2_verified==False):
  195. sq2=input("\nPlease enter your second security question: ")
  196. sq2a=input("Please type the answer for your second security question: ")
  197. if(first_run==True or sq3_verified==False):
  198. sq3=input("\nPlease enter your third security question: ")
  199. sq3a=input("Please type the answer for your third security question: ")
  200.  
  201. print("\nPlease take a moment to check the follwing questions: \n\n")
  202. print("Security Question : " + sq1 + "\nSecurity Question Answer: " + sq1a + "\n\n")
  203. print("Security Question : " + sq2 + "\nSecurity Question Answer: " + sq2a + "\n\n")
  204. print("Security Question : " + sq3 + "\nSecurity Question Answer: " + sq3a + "\n\n")
  205.  
  206. first_run=False
  207. security_question_check=input("If the FIRST question is INCORRECT type 1\nIf the SECOND question is INCORRECT press 2\nIf the THIRD question is INCORRECT press 3\n\nIf all of these question are CORRECT press [ENTER] ")
  208. if any((c in sq_verfication)for c in security_question_check):
  209. q1check = set("1one,")
  210. q2check = set("2two")
  211. q3check = set("3three")
  212. if any((c in q1check)for c in security_question_check):
  213. sq1_verified=False
  214. else:
  215. sq1_verified=True
  216. if any((c in q2check)for c in security_question_check):
  217. sq2_verified=False
  218. else:
  219. sq2_verified=True
  220. if any((c in q3check)for c in security_question_check):
  221. sq3_verified=False
  222. else:
  223. sq3_verified=True
  224. else:
  225. save(sq1, "./" + str(username) + "/sq1.txt.")
  226. save(sq2, "./" + str(username) + "/sq2.txt.")
  227. save(sq3, "./" + str(username) + "/sq3.txt.")
  228. save_as_encrypted(sq1a, "./" + str(username) + "/sq1a.txt.")
  229. save_as_encrypted(sq2a, "./" + str(username) + "/sq2a.txt.")
  230. save_as_encrypted(sq3a, "./" + str(username) + "/sq3a.txt.")
  231. security_question_creation=True
  232.  
  233. input("\nAccount successfuly created!\n\nPress [ENTER] to Exit")
  234. exit()
  235. else:
  236. print("Passwords do not match. Please try again.\n")
  237. time.sleep(1)
  238. else:
  239. print("Sorry that username is already taken! Please enter a different one.\n")
  240. time.sleep(1)
  241.  
  242. else:
  243. #username changing
  244. if(selection==3):
  245. old_username=input("Please enter your CURRENT username: ")
  246. if(os.path.exists("./" + old_username + "/")==True):
  247. password=read("./" + str(old_username) + "/password.txt")
  248. while(username_change_complete == False):
  249. if(int(old_attempts)==0):
  250. print("Too many unsuccessful attempts. Please try again later\n")
  251. time.sleep(3)
  252. input("\nPress [ENTER] to Exit")
  253. exit()
  254. else:
  255. if(int(old_attempts)==3 and old_delay==False):
  256. print("Too many unsuccessful attempts. Please try again in 30 seconds\n")
  257. old_delay=True
  258. time.sleep(password_delay)
  259. else:
  260. passcheck=encrypt((getpass.getpass("Please enter your PASSWORD (NOTE: Your input will be hidden): ")))
  261. if(password==passcheck):
  262. new_username=input("Please enter your NEW username. (NOTE: You may ONLY use characters a-z and it MUST be between 5 and 15 characters long): ")
  263. if len(new_username) > 15 or len(new_username) < 5:
  264. print("Username Must be between 5 and 15 characters long. Please try again.\n")
  265. else:
  266. os.rename(str(old_username), str(new_username))
  267. username_change_complete = True
  268. print("Username change successful")
  269. input("\nPress [ENTER] to Exit")
  270. exit()
  271. else:
  272. old_attempts=old_attempts - 1
  273. print("Password incorrect. You have " + str(old_attempts) + " attempts remaining\n")
  274. else:
  275. print("That username does not match. Please check you have typed it correctly or CREATE AN ACCOUNT if you havent already done so.\n")
  276.  
  277. else:
  278. #password changing
  279. if(selection==4):
  280. username=input("Please enter your USERNAME: ")
  281. if(os.path.exists("./" + username + "/")==True):
  282. old_password=read("./" + str(username) + "/password.txt")
  283. while(username_change_complete == False):
  284. if(int(old_attempts)==0):
  285. print("Too many unsuccessful attempts. Please try again later\n")
  286. time.sleep(3)
  287. input("\nPress [ENTER] to Exit")
  288. exit()
  289. else:
  290. if(int(old_attempts)==3 and old_delay==False):
  291. print("Too many unsuccessful attempts. Please try again in 30 seconds\n")
  292. old_delay=True
  293. time.sleep(password_delay)
  294. else:
  295. old_passcheck=encrypt((getpass.getpass("Please enter your CURRENT password (NOTE: Your input will be hidden): ")))
  296. if(old_password==old_passcheck):
  297. password=((getpass.getpass("Please enter your new password. It must contain AT LEAST 6 characters (NOTE: Your input will be hidden): ")))
  298. if len(password)<6:
  299. print("Password must be AT LEAST 6 characters long.")
  300. else:
  301. passcheck=((getpass.getpass("Please re-enter your new password (NOTE: Your input will be hidden): ")))
  302. if passcheck==password:
  303. save_as_encrypted(password, "./" + str(username) + "/password.txt")
  304. password_change_complete = True
  305. print("Password change successful")
  306. input("\nPress [ENTER] to Exit")
  307. exit()
  308. else:
  309. old_attempts=old_attempts - 1
  310. print("Old password incorrect. You have " + str(old_attempts) + " attempts remaining\n")
  311. else:
  312. print("That username does not match. Please check you have typed it correctly or CREATE AN ACCOUNT if you havent already done so.\n")
  313. else:
  314. if(selection==5):
  315. username=input("Please enter your USERNAME: ")
  316. if(os.path.exists("./" + str(username) + "/")==True):
  317. security_question=read("./" + str(username) + "/sq" + str(x) + ".txt")
  318. security_question_answer=read("./" + str(username) + "/sq" + str(x) + "a.txt")
  319. if(int(recovery_attempts!=0)):
  320. while(password_recovery_complete==False):
  321. user_answer=encrypt(input("Please answer the following security question: " + security_question + " "))
  322. if(user_answer== security_question_answer):
  323. print("Security question correct.")
  324. password_recovery_complete=True
  325. print("\nAt this point there would be an option to send a recovery email or something but I can't do that yet sorry!")
  326. input("\nPress [ENTER] to Exit")
  327. exit()
  328. else:
  329. revcovery_attempts=recovery_attempts-1
  330. print("Security question incorrect. You have " + str(attempts) + " attempts remaining to protect your data.")
  331. else:
  332. print("Too many unsuccessful attempts. Please try again later")
  333. time.sleep(3)
  334. exit()
  335. input("\nPress [ENTER] to Exit")
  336. exit()
  337. else:
  338. print("That username does not match. Please check you have typed it correctly or CREATE AN ACCOUNT if you havent already done so.\n")
  339.  
  340.  
  341. #anything you want when signed in goes here.
  342. print("You are now signed in!")
  343. input("\nPress [ENTER] to Exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement