TrollerBlader

Final Encrypted Login.py

Jun 11th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. from base64 import *
  2.  
  3. #=-=-=-=-=-=-=-=-=-=-=-=-#
  4. #                        #
  5. #      Login System      #
  6. #                        #
  7. #=-=-=-=-=-=-=-=-=-=-=-=-#
  8.  
  9. #Define the list of account to allow in:
  10. def txt_to_dict():
  11.     """The purpose of this function is to take
  12.     the information found in the account_info.txt
  13.     file and save it to a dictionary proper."""
  14.  
  15.     user_info = open("account_info.txt", "r")
  16.  
  17.  
  18.     raw_info = []
  19.     account_info = {}
  20.  
  21.     #Store the information into an array
  22.     for each in user_info:
  23.         temp = each.rstrip('\n')
  24.         raw_info.append(temp)
  25.  
  26.     user_info.close()
  27.        
  28.     #Take each chunk of text and seperate it proper
  29.  
  30.     for each in raw_info:
  31.         count = 0
  32.         if each[0] != " ":
  33.             while each[count] != ";":
  34.                 count += 1dt
  35.             if each[count] == ";":
  36.                 username = each[0:count]
  37.                 password = each[(count+1):len(each)]
  38.  
  39.                 account_info[username] = password
  40.  
  41.     #return raw_info
  42.     login(account_info)
  43.  
  44. def update_txt(account_list, username, password):
  45.     user_info = open("account_info.txt", "a")
  46.  
  47.     user_info.write(username + ";" + password + "\n")
  48.  
  49.     user_info.close()
  50.     create_txt(username)
  51.  
  52.  
  53. def string_encode(string):
  54.  
  55.     #Take the user string and encode it once:
  56.     stringEnc1 = ""
  57.     stringEnc1 = b64encode(string)
  58.  
  59.     #Then encode it again:
  60.     stringEnc2 = ""
  61.     stringEnc2 = b64encode(stringEnc1)
  62.     return stringEnc2
  63.  
  64.  
  65. def string_decode(string):
  66.  
  67.  
  68.     #Take the inputted string and decode it twice:
  69.     stringDec1 = ""
  70.     stringDec1 = b64decode(string)
  71.     stringDec2 = ""
  72.     stringDec2 = b64decode(stringDec1)
  73.  
  74.     return stringDec2
  75.  
  76. def login(account_list):
  77.  
  78.     print account_list
  79.  
  80.     """This function takes the user's login
  81.     information encrypt it using the string_encode
  82.     function. Then it runs the information through
  83.     a dictionary of account information."""
  84.  
  85.     #Get the user provided information (encrypting the password when receiving)
  86.  
  87.     account_username = raw_input(">>>   ")
  88.     account_password = string_encode(raw_input(">>>   "))
  89.  
  90.     #Make sure the username is lowercase:
  91.     account_username = account_username.lower()
  92.     account_username = string_encode(account_username)
  93.  
  94.     #Set up a local boolean to determine if the login
  95.     #was successful or not
  96.     login_success = False
  97.  
  98.     #Determine if the user wants to register with this
  99.     #local variable:
  100.     register = ""
  101.  
  102.     #Then run it through the dictionary of usernames
  103.     #Loop the dictionary's first key to check for the
  104.     #correct username, then do it again for the password:
  105.     for username in account_list:
  106.         if account_username == username:
  107.             if account_password == account_list[username]:
  108.                 print "Login successful"
  109.                 login_success = True
  110.                 data_menu(username)
  111.  
  112.     #If the login failed but the username is in the list,
  113.     #tell the user the password is incorrect.
  114.     if login_success == False and account_username in account_list:
  115.         print "Sorry, password incorrect."
  116.  
  117.  
  118.     if login_success == False and account_username not in account_list:
  119.         print "Sorry, username not recognized."
  120.         register = raw_input("Do you wish to register your information? y/n \n>>>   ")
  121.         if register.lower() == "y":
  122.             account_register(account_list)
  123.  
  124. def account_register(account_list):
  125.         #Clientside
  126.         """This function serves to take the user's
  127.        login information and append it to the account
  128.        list so he/she can sign in."""
  129.  
  130.         #Get the info to add
  131.  
  132.         user_to_add = raw_input("Username to add \n>>>   ").lower()
  133.         user_to_add = string_encode(user_to_add)
  134.         pass_to_add = string_encode(raw_input("Password to add \n>>>   "))
  135.         pass_to_add2 = string_encode(raw_input("Retype password \n>>>   "))
  136.  
  137.         #Double check (again) to make sure the username to add is NOT in the system:
  138.         if user_to_add in account_list:
  139.                 print "woah there! That username is already registered."
  140.  
  141.         #Make sure the two passwords match eachother!
  142.         if pass_to_add == pass_to_add2:
  143.                 print "Saved succesfully!"
  144.                 update_txt(account_list, user_to_add, pass_to_add)
  145.         else:
  146.                 print "The two passwords do not match."
  147.                 login(account_list)
  148.  
  149.         #Then return to the login screen
  150.         login(account_list)
Advertisement
Add Comment
Please, Sign In to add comment