Password Manager In Python

March 11, 2024, 5:20 a.m.

Password manager means that you store all your passwords, like YouTube, Facebook, and your personal password, securely, so you don't have to worry about remembering them.

Introduction:

Password manager means that you store all your passwords, like YouTube, Facebook, and your personal password, securely, so you don't have to worry about remembering them.That is a password manager application built using the Python Tkinter library.This provides a secure and simple way to store.This technology helps internet users create,save, manage, or use passwords across different online services.

Technology Requirements:

Used to implement Password managment system requires following technology:

  1. Python
  2. Django framework
  3. cryptography.farnet:This section used a python library becuase used for data encryption.
  4. IDE:Integrated Development Environment of your choice like VS Code,pycharm and any other.

Features:

The password manager must be able to handle many users and store the password and username for websites, apps, and games.

from cryptography.fernet import Fernet

class PasswordManager:

    def __init__(self):
        self.key=None
        self.password_file=None
        self.password_dict={}



    def create_key(self,path):
        self.key = Fernet.generate_key()
        # print(self.key)
        with open(path,'wb') as f:
            f.write(self.key)


# # create mykey.kev
# pm = PasswordManager()
# pm.create_key("mykey.kev") 
            
    def load_key(self,path):
        with open(path,'rb') as f:
            self.key=f.read()

    
    def create_password_file(self,path,initial_value=None):
        self.password_file=path

        if initial_value is not None:
            for key,value in initial_value.items():
                # pass #ToDO: add password function
                self.add_password(key,value)


    def load_password_file(self,path):
        self.password_file=path

        with open(path,'r') as f:
            for line in f:
                site, encrypted = line.split(":")
                self.password_dict[site]= Fernet(self.key).decrypt(encrypted.encode()).decode()

    def add_password(self,site,password):
        self.password_dict[site]=password

        if self.password_file is not None:
            with open(self.password_file,'a+') as f:
                encrypted = Fernet(self.key).encrypt(password.encode())
                f.write(site + "."+encrypted.decode()+"\n")


    def get_password(self,site):
        return self.password_dict[site]
    
def main():
        password={
            "email": "1234567",
            "facebook": "myfbpassword",
            "youtube":"helloworld123",
            "something":"myfavoritepassword_123"
        }

        pm  = PasswordManager()

        print("""what do you want to do 
              (1)Create a new key
              (2) Load a existing key
              (3) Create new password file
              (4) Add  existing password file
              (5) Get a password
              (q) quit
              """)
        
        done= False

        while not done:
            choice = input("Enter your choice:")
            if choice=="1":
                path = input("Enter path :")
                pm.create_key(path)
            elif choice=="2":
                path = input("Enter path:")
                pm.load_key(path)

            elif choice == "3":
                path = input("enter Path: ")
                pm.create_password_file(path,password)
            elif choice=="4":
                path = input("Enter path:")
                pm.load_password_file(path)
            elif choice=="5":
                site = input("Enter the site :")
                password= input("Enter the password:")
                pm.add_password(site,password)

            elif choice=="6":
                site = input("what site do you want:")
                print(f"Password for {site} is {pm.get_password(site)}")
            elif choice == "q":
                done=True
                print("Bye")
            else:
                print("Invalid choice !")

if __name__=="__main__":
    main()
        
        



Working this Code:

Firstly, import the library, like the fernet class, from the cryptography.fernet module. This class used to be.

pip install fernet

PasswordManager Class:

  • The __init__ method initializes the PasswordManger class with variables 'key', 'password_file', and 'password_dict'.
  • The create_key method generates a new encryption key and saves it.
  • Again, the load_key method loads a password from a password file.
  • create_password_file:create a new password file.
  • The load_password_file method loads passwords from a password file and decrypts them using the encryption key.
  • add_password adds a new password to the password_dict
  • get_password This method is used to retrieve the password.

Main function:

  • This method initializes a dictionary with some initial passwords.
  • Display a menu with options for the user to choose from.
  • and execute the different procedures based on user input.
  • and continues to loop until the user chooses to quit.

Execution:

  • When you run the program, it will display the option.
  • You can create a new encryption key, load an existing key, and create a new password file. Load a password from an existing file, then add passwords manually and retrieve the passwords.

pass

Depending on your choices, the code will perform actions such as generation of keys,saving and loading passwords, and displaying passwords.

Press the above action, then create a "testkey. key", "mypasswords.pass".

main

Encyption and decryption:

The program uses the fernet class for encryption and decryption.When your passwords are added or loaded, this key is encrypted using the encryption key. When passwords are retrieved, they are decrypted using the same key.

open mypasswords.pass andmypasswords.pass provide a decrypted password.

email.gAAAAABlx3Sl7JnGu1CZTjVkLRzNmL7xGpiSK0OaIdUVZr_oNVkC_C_M9ZdJ3Bh9LFF5tj1bMc-ijQ1X7DqWviLAF0umXmD-3w==
facebook.gAAAAABlx3SmIJtZlfbPCKg5vUwVTPQQ18M1KK7KpJZvbZ_ujph5nTbVDctgUMBcbnyQQOX1gERsRHO--RrKwagdZkzzoGKpuQ==
youtube.gAAAAABlx3SmrVGCKUa8mSK8GGWXe49LRZLjPNL1h-x6zoKVZ8R4zTvIgQvKLC9kraXO-jCp3yCVOjsvN9R9lD_52p4OwTQCFg==
something.gAAAAABlx3SmwXFjmStEfYdR7sfsQ72HZyZe3TNn_Iablr8vo-N-z4uveb7BtnDVXvwR1jdZisgKBNQnc-OtxhW0lQ5BWlaLZGOAB21AqpPii9Ig2m7V6bU=

This program provides basic password manager properties where users can manage their passwords using a command-line interface.That is the act of creating, loading, saving,adding, and retrieving passwords securely.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Get 2Lakh Prize Money

If you solve Easyalgo DSA sheet then you can get a chance to get a prize of Rs. 2 lakhs, you have to solve only 350 questions.!

Solve DSA Sheet

If you solve Easyalgo DSA sheet then you can get a chance to get a prize of Rs. 2 lakhs, you have to solve only 350 questions.!

Comments

sur
2 months ago
hi

Add Comment

Footer Design

Copyright © EasyAlgo All Rights Reserved