공부/파이썬 Python

비밀번호 관리 매니저 만들기 using Python

혼밥맨 2022. 3. 18. 15:30
반응형

 

비밀번호 관리 매니저 만들기 using Python

 

Introduction

우리는 여러 웹사이트에 회원 가입 했습니다. 여러 아이디, 여러 패스워드 .. 보안에 취약합니다. 

주기적인 비밀번호 변경 관리와 암호화가 필요합니다.

 

필요 라이브러리

1
pip install cryptography
cs

 - 비밀번호를 encode, decode하는 데에 필요한 라이브러리입니다. 

 

기능

파이썬으로 제작하는 비밀번호 관리 매니저는 key를 만들고, key를 로드하고, 패스워드 파일을 만들고, 패스워드 파일을 얻을 수 있습니다. 

Full Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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()
        with open(path, 'wb'as f:
            f.write(self.key)
 
    
    def load_key(self, path):
        with open(path, 'rb'as f:
            self.key = f.read()
 
    
    def create_password_file(self, path, initial_values=None):
        self.password_file = path    
 
        if initial_values is not None:
            for key, value in initial_values.items():
                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(":")
 
 
    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 an existing key
    (3) Create new password file
    (4) Load existing password file
    (5) Add a new password
    (6) 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":
            path = 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()
 
 
cs

 

실행

새로 생성한 key
새로 생성한 패스워드 파일

key와 패스워드 파일을 생성했습니다. 

반응형