简单的密码管理器(Python)(待完善)

需要使用的库:pymssql(用于连接SQL Server), PyQt5(用于窗口的制作)

 

首先编写DOS界面的密码生成器,以及将程序与数据库相连接,用于存储和查询

PasswordDOS.py

 1 from random import randint
 2 import pymssql
 3 
 4 
 5 def connect_to_sql():
 6     print('连接中...')
 7     connect = pymssql.connect('(local)', 'sa', '123456789', 'Password')
 8     if connect:
 9         print('连接成功...')
10     else:
11         print('未连接')
12         exit()
13     return connect
14 
15 
16 def rand_password():
17     whole_char = r'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ._!@#$%^&*'
18     char_number = len(whole_char)
19     password_length = input('please input password lenght: ')
20     password = []
21     for _ in range(0, int(password_length)):
22         password.append(whole_char[randint(0, char_number - 1)])
23     return password
24 
25 
26 def inquiry_password(cursor):
27     name = input('please input password for use: ')
28     if name == '':
29         sql = r"select * from password"
30     else:
31         sql = r"select * from password where id ='" + name + "'"
32     cursor.execute(sql)
33     result = cursor.fetchall()
34     if len(result) == 0:
35         print('不存在该密码')
36     else:
37         for value in result:
38             print(value[0]+'的密码是:'+value[1])
39 
40 
41 def add_password(connect, cursor):
42     name = input('please input password for use: ')
43     pwd = input('please input password: ')
44     if pwd == '':
45         pwd = ''.join(rand_password())
46     sql = r"insert into password values('" + name + "', '" + pwd + "')"
47     cursor.execute(sql)
48     connect.commit()
49     print(name + '的密码是:' + pwd)
50 
51 
52 def delete_password(connect, cursor):
53     name = input('please input password for use: ')
54     sql = r"delete from password where id='" + name + "'"
55     cursor.execute(sql)
56     connect.commit()
57 
58 
59 def change_password(connect, cursor):
60     name = input('please input password for use: ')
61     pwd = input('please input password: ')
62     if pwd == '':
63         pwd = ''.join(rand_password())
64     sql = r"update password set pwd='" + pwd + "'where id='" + name + "'"
65     cursor.execute(sql)
66     connect.commit()
67     print(name + '的密码是:' + pwd)
68 
69 
70 def run():
71     connect = connect_to_sql()
72     cursor = connect.cursor()
73     while True:
74         flag = input('please input (A/D/C/I/E{add,delete,change,inquiry,exit}): ')
75         if flag == 'A' or flag == 'a':
76             add_password(connect, cursor)
77         elif flag == 'D' or flag == 'd':
78             delete_password(connect, cursor)
79         elif flag == 'C' or flag == 'c':
80             change_password(connect, cursor)
81         elif flag == 'I' or flag == 'i':
82             inquiry_password(cursor)
83         elif flag == 'E' or flag == 'e':
84             break
85     connect.close()
86 
87 
88 if __name__ == '__main__':
89     run()

 

将DOS界面的程序进行稍微修改,用于窗口界面的导入文件

password.py

  1 from random import randint
  2 import pymssql
  3 
  4 
  5 def connect_to_sql():
  6     print('连接中...')
  7     connect = pymssql.connect('(local)', 'sa', '123456789', 'Password')
  8     if connect:
  9         print('连接成功')
 10     else:
 11         print('未连接')
 12         exit()
 13     return connect
 14 
 15 
 16 def rand_password(password_length):
 17     whole_char = r'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ._!@#$%^&*'
 18     char_number = len(whole_char)
 19     # password_length = input('please input password lenght: ')
 20     password = []
 21     for _ in range(0, int(password_length)):
 22         password.append(whole_char[randint(0, char_number - 1)])
 23     return password
 24 
 25 
 26 def inquiry_password(cursor, name):
 27     # name = input('please input password for use: ')
 28     if name == 'all' or name == 'ALL':
 29         sql = r"select * from password"
 30     elif name == '':
 31         return '请输入账号'
 32     else:
 33         sql = r"select * from password where id ='" + name + "'"
 34     cursor.execute(sql)
 35     result = cursor.fetchall()
 36     if len(result) == 0:
 37         # print('不存在该密码')
 38         return '不存在该密码'
 39     else:
 40         _: str = ''
 41         for value in result:
 42             # print(value[0]+'的密码是:'+value[1])
 43             _ += value[0]+'的密码是:'+value[1]+'\n'
 44         return _
 45 
 46 
 47 def add_password(connect, cursor, name, pwd, length=16):
 48     # name = input('please input password for use: ')
 49     # pwd = input('please input password: ')
 50     if pwd == '':
 51         pwd = ''.join(rand_password(length))
 52     sql = r"select id from password where id = '" + name + "'"
 53     cursor.execute(sql)
 54     PasswordId = cursor.fetchone()
 55     if PasswordId == (name, ):
 56             return '密码已存在,请使用修改'
 57     sql = r"insert into password values('" + name + "', '" + pwd + "')"
 58     cursor.execute(sql)
 59     connect.commit()
 60     # print(name + '的密码是:' + pwd)
 61     result = name + '的密码是:' + pwd
 62     return result
 63 
 64 
 65 def delete_password(connect, cursor, name):
 66     # name = input('please input password for use: ')
 67     if name == '':
 68         return '请输入账号'
 69     sql = r"select id from password where id = '" + name + "'"
 70     cursor.execute(sql)
 71     PasswordId = cursor.fetchone()
 72     if PasswordId is None:
 73         return '不存在' + name + '账号'
 74     sql = r"delete from password where id='" + name + "'"
 75     cursor.execute(sql)
 76     connect.commit()
 77     return '已删除' + name + '账号'
 78 
 79 
 80 def change_password(connect, cursor, name, pwd, length=16):
 81     # name = input('please input password for use: ')
 82     # pwd = input('please input password: ')
 83     if pwd == '':
 84         pwd = ''.join(rand_password(length))
 85     sql = r"update password set pwd='" + pwd + "'where id='" + name + "'"
 86     cursor.execute(sql)
 87     connect.commit()
 88     # print(name + '的密码是:' + pwd)
 89     result = name + '的密码是:' + pwd
 90     return result
 91 
 92 
 93 # noinspection PyArgumentList
 94 def run():
 95     connect = connect_to_sql()
 96     cursor: connect.cursor = connect.cursor()
 97     while True:
 98         flag = input('please input (A/D/C/I/E{add,delete,change,inquiry,exit}): ')
 99         if flag == 'A' or flag == 'a':
100             add_password(connect, cursor)
101         elif flag == 'D' or flag == 'd':
102             delete_password(connect, cursor)
103         elif flag == 'C' or flag == 'c':
104             change_password(connect, cursor)
105         elif flag == 'I' or flag == 'i':
106             inquiry_password(cursor)
107         elif flag == 'E' or flag == 'e':
108             break
109     connect.close()
110 
111 
112 if __name__ == '__main__':
113     run()

 

使用PyQt5库编写窗口程序

IPassword.py

  1 # import os
  2 import sys
  3 from PyQt5.QtGui import QIcon, QFont
  4 from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QDesktopWidget, QLabel, QLineEdit, QPushButton,\
  5     QVBoxLayout, QHBoxLayout, QTextEdit
  6 from password import *
  7 
  8 
  9 class Window(QWidget):
 10     def __init__(self, **kwargs):
 11         # noinspection PyArgumentList
 12         super().__init__()
 13         self.Label = []
 14         self.LineEdit = []
 15         self.Button = []
 16         self.TextEdit = []
 17         self.connect = kwargs['connect']
 18         self.cursor = kwargs['cursor']
 19         self.initUi()
 20         self.setFixedSize(self.width(), self.height())  # 固定窗口的大小为其初始大小
 21 
 22     # noinspection PyArgumentList
 23     def initUi(self):
 24         # noinspection PyCallByClass
 25         QToolTip.setFont(QFont("SansSerif", 10))
 26         self.resize(240, 360)
 27         self.frameGeometry().moveCenter(QDesktopWidget().availableGeometry().center())
 28         self.setWindowTitle("IPassword")
 29         self.setWindowIcon(QIcon(r"src/IPassword.svg"))
 30         self.Label.append(QLabel(self))
 31         self.Label[0].setText('账  号:')
 32         self.Label.append(QLabel(self))
 33         self.Label[1].setText('密  码:')
 34         self.LineEdit.append(QLineEdit())
 35         self.LineEdit[0].setText('')
 36         self.LineEdit.append(QLineEdit())
 37         self.LineEdit[1].setText('')
 38         # self.LineEdit[0].setHidden(True)
 39         # self.LineEdit[0].setVisible(False)
 40         self.Button.append(QPushButton('增加', self))
 41         self.Button.append(QPushButton('删除', self))
 42         self.Button.append(QPushButton('修改', self))
 43         self.Button.append(QPushButton('查询', self))
 44         self.Button[0].clicked.connect(lambda: self.addInfo())
 45         self.Button[1].clicked.connect(lambda: self.deleteInfo())
 46         self.Button[2].clicked.connect(lambda: self.changeInfo())
 47         self.Button[3].clicked.connect(lambda: self.getInfo())
 48         self.TextEdit.append(QTextEdit(self))
 49         self.TextEdit[0].setPlainText('......')
 50 
 51         hbox1 = QHBoxLayout()
 52         hbox1.addWidget(self.Label[0])
 53         hbox1.addStretch(1)
 54         hbox1.addWidget(self.LineEdit[0])
 55         hbox2 = QHBoxLayout()
 56         hbox2.addWidget(self.Label[1])
 57         hbox2.addStretch(1)
 58         hbox2.addWidget(self.LineEdit[1])
 59         hbox3 = QHBoxLayout()
 60         hbox3.addWidget(self.TextEdit[0])
 61         hbox4 = QHBoxLayout()
 62         hbox4.addWidget(self.Button[0])
 63         hbox4.addWidget(self.Button[1])
 64         hbox5 = QHBoxLayout()
 65         hbox5.addWidget(self.Button[2])
 66         hbox5.addWidget(self.Button[3])
 67 
 68         vbox = QVBoxLayout()
 69         vbox.addLayout(hbox1)
 70         vbox.addLayout(hbox2)
 71         vbox.addLayout(hbox3)
 72         vbox.addLayout(hbox4)
 73         vbox.addLayout(hbox5)
 74 
 75         self.setLayout(vbox)
 76         self.show()
 77 
 78     def addInfo(self):
 79         PasswordInfo = add_password(self.connect, self.cursor, self.LineEdit[0].text(), self.LineEdit[1].text())
 80         self.LineEdit[0].setText('')
 81         self.LineEdit[1].setText('')
 82         self.TextEdit[0].setPlainText(PasswordInfo)
 83 
 84     def deleteInfo(self):
 85         PasswordInfo = delete_password(self.connect, self.cursor, self.LineEdit[0].text())
 86         self.LineEdit[0].setText('')
 87         self.LineEdit[1].setText('')
 88         self.TextEdit[0].setPlainText(PasswordInfo)
 89 
 90     def changeInfo(self):
 91         PasswordInfo = change_password(self.connect, self.cursor, self.LineEdit[0].text(), self.LineEdit[1].text())
 92         self.LineEdit[0].setText('')
 93         self.LineEdit[1].setText('')
 94         self.TextEdit[0].setPlainText(PasswordInfo)
 95 
 96     def getInfo(self):
 97         PasswordInfo = inquiry_password(self.cursor, self.LineEdit[0].text())
 98         self.LineEdit[0].setText('')
 99         self.LineEdit[1].setText('')
100         self.TextEdit[0].setPlainText(PasswordInfo)
101 
102     def closeEvent(self, CloseEvent):
103         print('断开中...')
104         self.connect.close()
105         print('已断开')
106         print('关闭中...')
107         CloseEvent.accept()
108 
109 
110 def run():
111     print('启动中...')
112     connect = connect_to_sql()
113     cursor = connect.cursor()
114     application = QApplication(sys.argv)
115     _ = Window(connect=connect, cursor=cursor)
116     print('启动成功')
117     try:
118         sys.exit(application.exec())
119     except SystemExit as _:
120         print('退出成功')
121 
122 
123 if __name__ == '__main__':
124     run()

 

暂时可以实现密码的生成、存储、修改、查询、删除的功能。

时间:2019-08-20
状态:未完成(半成品)
作者:Wzz

 

posted @ 2019-08-20 17:53  菜鸟plus  阅读(885)  评论(0编辑  收藏  举报