0% found this document useful (0 votes)
25 views

Question - 3

Uploaded by

Raman Bajaj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Question - 3

Uploaded by

Raman Bajaj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

QUESTION – 3:

Create a graphical user interface application that can be used to store the
password along with the username. All the data will be encrypted to prove
better security and will be decrypted on demand. Every password will also be
hidden behind a wall which we can enter by using the application password.

SOLUTION:

SOURCE CODE

import tkinter as tk
from tkinter import messagebox, Toplevel, Label, Entry, Button
from cryptography.fernet import Fernet
import json
import os
import hashlib

# Generate encryption key if not present


if not os.path.exists('key.key'):
with open('key.key', 'wb') as key_file:
key_file.write(Fernet.generate_key())
with open('key.key', 'rb') as key_file:
key = key_file.read()
cipher = Fernet(key)

# Initialize data storage


data_file = 'passwords.json'
if not os.path.exists(data_file):
with open(data_file, 'w') as f:
json.dump({}, f)

# Hash function for application password


def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()

class PasswordManager:
def __init__(self, root):
self.root = root
self.root.title("Password Manager")
self.root.geometry("400x400")
self.root.configure(bg="#2e3b4e") # Background color for the app

self.app_password = None
self.load_app_password()

def load_app_password(self):
# Load or create a master password
if os.path.exists("app_password.txt"):
with open("app_password.txt", "r") as f:
self.app_password = f.read().strip()
self.main_screen() # Load main screen if password exists
else:
self.setup_password() # Prompt to set password if it doesn't exist
def setup_password(self):
# Create a new window for setting up the application password
setup_window = Toplevel(self.root)
setup_window.title("Setup Application Password")
setup_window.geometry("300x200")
setup_window.configure(bg="#2e3b4e")

# Add styling and prompts to guide the user


Label(setup_window, text="Set Up Application Password", font=("Arial", 14,
"bold"), fg="#f7f7f7", bg="#2e3b4e").pack(pady=10)
Label(setup_window, text="Enter a secure password to protect your data.",
font=("Arial", 10), fg="#f7f7f7", bg="#2e3b4e", wraplength=250).pack(pady=5)

# Input field for setting the password


password_entry = Entry(setup_window, show="*", width=25, font=("Arial",
12))
password_entry.pack(pady=10)

def save_setup_password():
password = password_entry.get()
if password:
self.app_password = hash_password(password)
with open("app_password.txt", "w") as f:
f.write(self.app_password)
messagebox.showinfo("Success", "Application password set
successfully!")
setup_window.destroy()
self.main_screen() # Proceed to main screen
else:
messagebox.showerror("Error", "Password cannot be empty")

# Button to save the setup password


save_button = Button(setup_window, text="Set Password",
command=save_setup_password, bg="#4a90e2", fg="white", font=("Arial", 12),
width=12)
save_button.pack(pady=10)

def main_screen(self):
self.clear_window()

tk.Label(self.root, text="Password Manager", font=("Arial", 18, "bold"),


fg="#f7f7f7", bg="#2e3b4e").pack(pady=10)
tk.Label(self.root, text="Enter your application password to access the
vault.", font=("Arial", 10), fg="#f7f7f7", bg="#2e3b4e",
wraplength=300).pack(pady=5)

self.password_entry = tk.Entry(self.root, show="*", width=25, font=("Arial",


12))
self.password_entry.pack(pady=10)

login_button = tk.Button(self.root, text="Login", command=self.login,


bg="#4a90e2", fg="white", font=("Arial", 12), width=10)
login_button.pack(pady=10)
def login(self):
entered_password = hash_password(self.password_entry.get())

if entered_password == self.app_password:
self.password_vault()
else:
messagebox.showerror("Error", "Incorrect application password")

def password_vault(self):
self.clear_window()

tk.Label(self.root, text="Password Vault", font=("Arial", 18, "bold"),


fg="#f7f7f7", bg="#2e3b4e").pack(pady=10)
tk.Label(self.root, text="Manage your saved usernames and passwords
securely.\n\nChoose an option below:", font=("Arial", 10), fg="#f7f7f7",
bg="#2e3b4e", wraplength=300).pack(pady=5)

add_button = tk.Button(self.root, text="Add New Entry",


command=self.add_entry, bg="#4a90e2", fg="white", font=("Arial", 12),
width=15)
add_button.pack(pady=5)

view_button = tk.Button(self.root, text="View Entries",


command=self.view_entries, bg="#4a90e2", fg="white", font=("Arial", 12),
width=15)
view_button.pack(pady=5)

def add_entry(self):
add_window = Toplevel(self.root)
add_window.title("Add New Entry")
add_window.geometry("300x200")
add_window.configure(bg="#2e3b4e")

Label(add_window, text="Add a New Password Entry", font=("Arial", 14,


"bold"), fg="#f7f7f7", bg="#2e3b4e").pack(pady=10)

Label(add_window, text="Username:", font=("Arial", 10), fg="#f7f7f7",


bg="#2e3b4e").pack()
username_entry = Entry(add_window, width=25, font=("Arial", 12))
username_entry.pack(pady=5)

Label(add_window, text="Password:", font=("Arial", 10), fg="#f7f7f7",


bg="#2e3b4e").pack()
password_entry = Entry(add_window, show="*", width=25, font=("Arial",
12))
password_entry.pack(pady=5)

def save_entry():
username = username_entry.get()
password = password_entry.get()

if username and password:


encrypted_password = cipher.encrypt(password.encode()).decode()

with open(data_file, 'r') as f:


data = json.load(f)

data[username] = encrypted_password

with open(data_file, 'w') as f:


json.dump(data, f)

messagebox.showinfo("Success", "Entry added successfully")


add_window.destroy()
else:
messagebox.showerror("Error", "Please fill in both fields")

save_button = Button(add_window, text="Save Entry",


command=save_entry, bg="#4a90e2", fg="white", font=("Arial", 12), width=12)
save_button.pack(pady=10)

def view_entries(self):
with open(data_file, 'r') as f:
data = json.load(f)

self.clear_window()

tk.Label(self.root, text="Stored Entries", font=("Arial", 14, "bold"),


fg="#f7f7f7", bg="#2e3b4e").pack(pady=10)
tk.Label(self.root, text="Below are your stored usernames and
passwords.\nEach password is decrypted only when viewed here.",
font=("Arial", 10), fg="#f7f7f7", bg="#2e3b4e", wraplength=300).pack(pady=5)
for username, encrypted_password in data.items():
decrypted_password =
cipher.decrypt(encrypted_password.encode()).decode()
tk.Label(self.root, text=f"Username: {username}", font=("Arial", 12),
fg="#f7f7f7", bg="#2e3b4e").pack()
tk.Label(self.root, text=f"Password: {decrypted_password}", font=("Arial",
12), fg="#f7f7f7", bg="#2e3b4e").pack()
tk.Label(self.root, text="", bg="#2e3b4e").pack() # Empty line for spacing

back_button = tk.Button(self.root, text="Back",


command=self.password_vault, bg="#4a90e2", fg="white", font=("Arial", 12),
width=10)
back_button.pack(pady=10)

def clear_window(self):
for widget in self.root.winfo_children():
widget.destroy()

if __name__ == "__main__":
root = tk.Tk()
app = PasswordManager(root)
root.mainloop()
OUTPUT

Setup the Application Password


Entering the Application Password to access the database
Adding a New Entry:
Viewing the Entries:

You might also like