0

I am very new to python and could use a little help, I created a password locker program to store and recall accounts, logins and passwords for various accounts which requires 1 password to view all the stored info. this is my first program involving the pickle module and could use some input on how to increase my efficiency in using it, also is there a way to increase the encryption of the stored info so words can't be seen in the pickle file. Thanks!

here is what I have going on.

#! python3

import sys
import pickle

PIK = "pickle.pkl"

account = []

login = []

password = []    

print('What is your password?')
while True:
    p = input() 
    if p == 'springtime':
        break
    else:
        print('Incorrect password')
        continue

while True:
    try:
        with open(PIK, 'rb') as f:
            f.close()
            break
    except FileNotFoundError:
        with open(PIK, 'ab') as f:
            pickle.dump(account, f)
            pickle.dump(login, f)

            pickle.dump(password, f)
            f.close()
            break

with open(PIK, 'rb') as f:      
    while True:
        try:           
            account = pickle.load(f)

            login = pickle.load(f)
            password = pickle.load(f)
        except EOFError:
            break


def add():
    while True:
        print('What type of account is this? CANCEL to menu')
        y = input()
        if y.lower() == 'cancel':
            menu()
            break
        elif y.lower() != 'cancel':
            print('Are you sure this is the account type?(y/n): ' + y)
            confirm = input()
            if confirm.lower() == 'y':
                account.append(y)
                break
            else:
                continue

    while True:
        print('What is the login for the account? CANCEL to menu')
        x = input()
        if x.lower() == 'cancel':
            menu()
            break

        elif x.lower() != 'cancel':
            print('Are you sure this is the login?(y/n): ' + x)
            confirm = input()
            if confirm.lower() == 'y':
                login.append(x)
                break
            else:
                continue

    while True:    
        print('What is the password for the account? CANCEL to menu')
        z = input()
        if z.lower() == 'cancel':
            menu()
        elif x.lower != 'cancel':
            print('Are you sure this is the password?(y/n): ' + z)
            confirm = input()
            if confirm.lower() == 'y':
                password.append(z)
                print('Would you like to create another account?(y/n)')
                answer = input()
                if answer.lower() == 'y':
                    add()
                    break
                elif answer.lower() == 'n':
                    with open(PIK, 'ab') as f:
                        pickle.dump(account, f)
                        pickle.dump(login, f)
                        pickle.dump(password, f)
                        f.close()
                        menu()
                        break                   
        elif confirm.lower() == 'n':
            menu()
            break


def delete():
        snap()
        print('What account would you like to delete? (1-100) CANCEL to menu')
        selection = input()
        if selection.lower() == 'cancel':

            menu()
        else:
            try:
                result = int(selection)-1
                del account[result]
                del login[result]
                del password[result]
                with open(PIK, 'ab') as f:
                    pickle.dump(account, f)
                    pickle.dump(login, f)
                    pickle.dump(password, f)
                    f.close()
            except IndexError:
                print('account does not exist, try again')
                delete()     
            except ValueError:
                print('Please select account # or CANCEL')
                delete()
            print('Would you like to delete another account?(y/n)')
            d = input()
            if d.lower() == 'y':
                delete()
            elif d.lower() == 'n':
                menu()


def view():
    snap()
    menu()

def menu():
    print('Would you like to DELETE, ADD, or VIEW an account? (enter to EXIT)')
    d = input()
    if d.lower() == 'delete':
        delete()
    elif d.lower() == 'add':
        add()
    elif d.lower() == 'view':
        view()
    elif d == '':
        exit()
    else:
        menu()

def snap():
    for i in range(len(account)):
        print('\n*******', i+1, '*******\nACCOUNT: ', account[i], '\nLOGIN: ', login[i], '\nPASSWORD: ', password[i], '\n')     
menu()
DsBurton
  • 1
  • 2
  • While this is a fine pet project, **please don't use it to store actual passwords**. My tip would be **don't do it**. – TemporalWolf Jan 11 '18 at 22:39
  • I think this question is better suited for [Code Review](https://codereview.stackexchange.com/), because there doesn't actually seem to be a question here. – Aran-Fey Jan 11 '18 at 22:40
  • @Rawing Code Review cannot magically fix that. It's not on topic there, either. Is it? – jpaugh Jan 11 '18 at 22:45
  • @DsBurton There's probably a recommended best practice for storing passwords in Python. It will either involve a binding to a C library, or using BCrypt before pickling. – jpaugh Jan 11 '18 at 22:47
  • @DsBurton In fact, it's been asked on SO before. Choose [pre-cooked](https://stackoverflow.com/q/19147789/712526) or [roll-your-own](https://stackoverflow.com/q/6058019/712526). – jpaugh Jan 11 '18 at 22:49
  • Possible duplicate of [Best practices for storing passwords in GAE/python](https://stackoverflow.com/questions/19147789/best-practices-for-storing-passwords-in-gae-python) – jpaugh Jan 11 '18 at 22:50
  • cPickle can be much faster. Note: you must be using cPython and not PyPy – lwileczek Jan 11 '18 at 22:56
  • @lwileczek: There is no `cPickle` any more in Python 3. The C implementation still exists, but it's imported automatically for you when you request the normal `pickle` module. – Blckknght Jan 11 '18 at 22:58
  • Oh snap! I'm caught in the past. Thank you for telling me. – lwileczek Jan 11 '18 at 22:59
  • @TemporalWolf this is purely just a test program to better understand python. I don't plan on storing any real information. I've only been coding with python for a month so this question is mostly me being unsure if i'm using the pickle module correctly or if i'm being redundant in in my usage of it, also if there is a way to further encrypt the information stored in the pickle file. after reading I see that encryption is better done in other ways. – DsBurton Jan 11 '18 at 23:15
  • That's twice you've said 'further encrypt' in relation to pickle. Pickling does not encrypt in any way. If you want the pickled data to be encrypted then you pass encrypted data to pickle.dump(). Another option is to pickle.dumps() to a string, encrypt that and write it to a file. Perhaps an interesting exercise, but as mentioned don't trust real passwords to this method. – alaricljs Jan 12 '18 at 01:58

0 Answers0