|
| 1 | +importjson,hashlib,getpass,os,pyperclip,sys |
| 2 | +fromcryptography.fernetimportFernet |
| 3 | + |
| 4 | + |
| 5 | +# Function for Hashing the Master Password. |
| 6 | +defhash_password(password): |
| 7 | +sha256=hashlib.sha256() |
| 8 | +sha256.update(password.encode()) |
| 9 | +returnsha256.hexdigest() |
| 10 | + |
| 11 | + |
| 12 | +# Generate a secret key. This should be done only once as you'll see. |
| 13 | +defgenerate_key(): |
| 14 | +returnFernet.generate_key() |
| 15 | + |
| 16 | + |
| 17 | +# Initialize Fernet cipher with the provided key. |
| 18 | +definitialize_cipher(key): |
| 19 | +returnFernet(key) |
| 20 | + |
| 21 | + |
| 22 | +# Function to encrypt a password. |
| 23 | +defencrypt_password(cipher,password): |
| 24 | +returncipher.encrypt(password.encode()).decode() |
| 25 | + |
| 26 | + |
| 27 | +# Function to decrypt a password. |
| 28 | +defdecrypt_password(cipher,encrypted_password): |
| 29 | +returncipher.decrypt(encrypted_password.encode()).decode() |
| 30 | + |
| 31 | + |
| 32 | +# Function to register you. |
| 33 | +defregister(username,master_password): |
| 34 | +# Encrypt the master password before storing it |
| 35 | +hashed_master_password=hash_password(master_password) |
| 36 | +user_data= {'username':username,'master_password':hashed_master_password} |
| 37 | +file_name='user_data.json' |
| 38 | + |
| 39 | +ifos.path.exists(file_name)andos.path.getsize(file_name)==0: |
| 40 | +withopen(file_name,'w')asfile: |
| 41 | +json.dump(user_data,file) |
| 42 | +print("\n[+] Registration complete!!\n") |
| 43 | +else: |
| 44 | +withopen(file_name,'x')asfile: |
| 45 | +json.dump(user_data,file) |
| 46 | +print("\n[+] Registration complete!!\n") |
| 47 | + |
| 48 | + |
| 49 | +# Function to log you in. |
| 50 | +deflogin(username,entered_password): |
| 51 | +try: |
| 52 | +withopen('user_data.json','r')asfile: |
| 53 | +user_data=json.load(file) |
| 54 | + |
| 55 | +stored_password_hash=user_data.get('master_password') |
| 56 | +entered_password_hash=hash_password(entered_password) |
| 57 | + |
| 58 | +ifentered_password_hash==stored_password_hashandusername==user_data.get('username'): |
| 59 | +print("\n[+] Login Successful..\n") |
| 60 | +else: |
| 61 | +print("\n[-] Invalid Login credentials. Please use the credentials you used to register.\n") |
| 62 | +sys.exit() |
| 63 | + |
| 64 | +exceptException: |
| 65 | +print("\n[-] You have not registered. Please do that.\n") |
| 66 | +sys.exit() |
| 67 | + |
| 68 | + |
| 69 | +# Function to view saved websites. |
| 70 | +defview_websites(): |
| 71 | +try: |
| 72 | +withopen('passwords.json','r')asdata: |
| 73 | +view=json.load(data) |
| 74 | +print("\nWebsites you saved...\n") |
| 75 | +forxinview: |
| 76 | +print(x['website']) |
| 77 | +print('\n') |
| 78 | +exceptFileNotFoundError: |
| 79 | +print("\n[-] You have not saved any passwords!\n") |
| 80 | + |
| 81 | + |
| 82 | +# Load or generate the encryption key. |
| 83 | +key_filename='encryption_key.key' |
| 84 | +ifos.path.exists(key_filename): |
| 85 | +withopen(key_filename,'rb')askey_file: |
| 86 | +key=key_file.read() |
| 87 | +else: |
| 88 | +key=generate_key() |
| 89 | +withopen(key_filename,'wb')askey_file: |
| 90 | +key_file.write(key) |
| 91 | + |
| 92 | +cipher=initialize_cipher(key) |
| 93 | + |
| 94 | + |
| 95 | +# Function to add (save password). |
| 96 | +defadd_password(website,password): |
| 97 | +# Check if passwords.json exists |
| 98 | +ifnotos.path.exists('passwords.json'): |
| 99 | +# If passwords.json doesn't exist, initialize it with an empty list |
| 100 | +data= [] |
| 101 | +else: |
| 102 | +# Load existing data from passwords.json |
| 103 | +try: |
| 104 | +withopen('passwords.json','r')asfile: |
| 105 | +data=json.load(file) |
| 106 | +exceptjson.JSONDecodeError: |
| 107 | +# Handle the case where passwords.json is empty or invalid JSON. |
| 108 | +data= [] |
| 109 | + |
| 110 | +# Encrypt the password |
| 111 | +encrypted_password=encrypt_password(cipher,password) |
| 112 | + |
| 113 | +# Create a dictionary to store the website and password |
| 114 | +password_entry= {'website':website,'password':encrypted_password} |
| 115 | +data.append(password_entry) |
| 116 | + |
| 117 | +# Save the updated list back to passwords.json |
| 118 | +withopen('passwords.json','w')asfile: |
| 119 | +json.dump(data,file,indent=4) |
| 120 | + |
| 121 | + |
| 122 | +# Function to retrieve a saved password. |
| 123 | +defget_password(website): |
| 124 | +# Check if passwords.json exists |
| 125 | +ifnotos.path.exists('passwords.json'): |
| 126 | +returnNone |
| 127 | + |
| 128 | +# Load existing data from passwords.json |
| 129 | +try: |
| 130 | +withopen('passwords.json','r')asfile: |
| 131 | +data=json.load(file) |
| 132 | +exceptjson.JSONDecodeError: |
| 133 | +data= [] |
| 134 | +# Loop through all the websites and check if the requested website exists. |
| 135 | +forentryindata: |
| 136 | +ifentry['website']==website: |
| 137 | +# Decrypt and return the password |
| 138 | +decrypted_password=decrypt_password(cipher,entry['password']) |
| 139 | +returndecrypted_password |
| 140 | + |
| 141 | +returnNone |
| 142 | + |
| 143 | + |
| 144 | +# Infinite loop to keep the program running until the user chooses to quit. |
| 145 | +whileTrue: |
| 146 | +print("1. Register") |
| 147 | +print("2. Login") |
| 148 | +print("3. Quit") |
| 149 | +choice=input("Enter your choice: ") |
| 150 | + |
| 151 | +ifchoice=='1':# If a user wants to register |
| 152 | +file='user_data.json' |
| 153 | +ifos.path.exists(file)andos.path.getsize(file)!=0: |
| 154 | +print("\n[-] Master user already exists!!") |
| 155 | +sys.exit() |
| 156 | +else: |
| 157 | +username=input("Enter your username: ") |
| 158 | +master_password=getpass.getpass("Enter your master password: ") |
| 159 | +register(username,master_password) |
| 160 | + |
| 161 | +elifchoice=='2':# If a User wants to log in |
| 162 | +file='user_data.json' |
| 163 | +ifos.path.exists(file): |
| 164 | +username=input("Enter your username: ") |
| 165 | +master_password=getpass.getpass("Enter your master password: ") |
| 166 | +login(username,master_password) |
| 167 | +else: |
| 168 | +print("\n[-] You have not registered. Please do that.\n") |
| 169 | +sys.exit() |
| 170 | +# Various options after a successful Login. |
| 171 | +whileTrue: |
| 172 | +print("1. Add Password") |
| 173 | +print("2. Get Password") |
| 174 | +print("3. View Saved websites") |
| 175 | +print("4. Quit") |
| 176 | + |
| 177 | +password_choice=input("Enter your choice: ") |
| 178 | +ifpassword_choice=='1':# If a user wants to add a password |
| 179 | +website=input("Enter website: ") |
| 180 | +password=getpass.getpass("Enter password: ") |
| 181 | + |
| 182 | +# Encrypt and add the password |
| 183 | +add_password(website,password) |
| 184 | +print("\n[+] Password added!\n") |
| 185 | + |
| 186 | +elifpassword_choice=='2':# If a User wants to retrieve a password |
| 187 | +website=input("Enter website: ") |
| 188 | +decrypted_password=get_password(website) |
| 189 | +ifwebsiteanddecrypted_password: |
| 190 | +# Copy password to clipboard for convenience |
| 191 | +pyperclip.copy(decrypted_password) |
| 192 | +print(f"\n[+] Password for{website}:{decrypted_password}\n[+] Password copied to clipboard.\n") |
| 193 | +else: |
| 194 | +print("\n[-] Password not found! Did you save the password?" |
| 195 | +"\n[-] Use option 3 to see the websites you saved.\n") |
| 196 | + |
| 197 | +elifpassword_choice=='3':# If a user wants to view saved websites |
| 198 | +view_websites() |
| 199 | + |
| 200 | +elifpassword_choice=='4':# If a user wants to quit the password manager |
| 201 | +break |
| 202 | + |
| 203 | +elifchoice=='3':# If a user wants to quit the program |
| 204 | +break |