TrollerBlader

Final Encrypted Login.py

Jun 11th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     """This function takes the user's login
  79.     information encrypt it using the string_encode
  80.     function. Then it runs the information through
  81.     a dictionary of account information."""
  82.  
  83.     #Get the user provided information (encrypting the password when receiving)
  84.  
  85.     account_username = raw_input(">>>   ")
  86.     account_password = string_encode(raw_input(">>>   "))
  87.  
  88.     #Make sure the username is lowercase:
  89.     account_username = account_username.lower()
  90.     account_username = string_encode(account_username)
  91.  
  92.     #Set up a local boolean to determine if the login
  93.     #was successful or not
  94.     login_success = False
  95.  
  96.     #Determine if the user wants to register with this
  97.     #local variable:
  98.     register = ""
  99.  
  100.     #Then run it through the dictionary of usernames
  101.     #Loop the dictionary's first key to check for the
  102.     #correct username, then do it again for the password:
  103.     for username in account_list:
  104.         if account_username == username:
  105.             if account_password == account_list[username]:
  106.                 print "Login successful"
  107.                 login_success = True
  108.                 data_menu(username)
  109.  
  110.     #If the login failed but the username is in the list,
  111.     #tell the user the password is incorrect.
  112.     if login_success == False and account_username in account_list:
  113.         print "Sorry, password incorrect."
  114.  
  115.  
  116.     if login_success == False and account_username not in account_list:
  117.         print "Sorry, username not recognized."
  118.         register = raw_input("Do you wish to register your information? y/n \n>>>   ")
  119.         if register.lower() == "y":
  120.             account_register(account_list)
  121.  
  122. def account_register(account_list):
  123.         #Clientside
  124.         """This function serves to take the user's
  125.        login information and append it to the account
  126.        list so he/she can sign in."""
  127.  
  128.         #Get the info to add
  129.  
  130.         user_to_add = raw_input("Username to add \n>>>   ").lower()
  131.         user_to_add = string_encode(user_to_add)
  132.         pass_to_add = string_encode(raw_input("Password to add \n>>>   "))
  133.         pass_to_add2 = string_encode(raw_input("Retype password \n>>>   "))
  134.  
  135.         #Double check (again) to make sure the username to add is NOT in the system:
  136.         if user_to_add in account_list:
  137.                 print "woah there! That username is already registered."
  138.  
  139.         #Make sure the two passwords match eachother!
  140.         if pass_to_add == pass_to_add2:
  141.                 print "Saved succesfully!"
  142.                 update_txt(account_list, user_to_add, pass_to_add)
  143.         else:
  144.                 print "The two passwords do not match."
  145.                 login(account_list)
  146.  
  147.         #Then update the dictionary!
  148.         txt_to_dict()
Advertisement
Add Comment
Please, Sign In to add comment