简单的密码管理器(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‘[email protected]#$%^&*‘
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‘[email protected]#$%^&*‘
 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

原文地址:https://www.cnblogs.com/wzzdeblog/p/11384541.html

时间: 2024-12-31 03:13:05

简单的密码管理器(Python)(待完善)的相关文章

Keeweb-Linux的密码管理器

如今,我们依赖于越来越多的线上服务.我们每注册一个线上服务,就要设置一个密码:如此,我们就不得不记住数以百计的密码.这样对于每个人来说,都很容易忘记密码.那么,下面兄弟连(www.lampbrother.net )将在本文中为大家介绍 Keeweb,Keeweb是一款 Linux 下的密码管理器,可以为你离线或在线地安全存储所有的密码. 当谈及 Linux 密码管理器时,我们会发现有很多这样的软件.我们已经在 LinuxAndUbuntu 上讨论过像 Keepass 和 Encryptr,一个基

【兄弟连IT技术交流分享】Keeweb-Linux的密码管理器

如今,我们依赖于越来越多的线上服务.我们每注册一个线上服务,就要设置一个密码:如此,我们就不得不记住数以百计的密码.这样对于每个人来说,都很容易忘记密码.那么,下面将在本文中为大家介绍 Keeweb,Keeweb是一款 Linux 下的密码管理器,可以为你离线或在线地安全存储所有的密码. 当谈及 Linux 密码管理器时,我们会发现有很多这样的软件.我们已经在 LinuxAndUbuntu 上讨论过像 Keepass 和 Encryptr,一个基于零知识系统的密码管理器 这样的密码管理器.Kee

8种最好的Linux密码管理器

推荐8种最好的Linux密码管理器,可以安全地存储你的登录信息. ·Fiagaro's Password Manager·Gpass·Gpassword Manager·Gringotts·KeePassX·MyPasswords·PasswordSafe·Revelation 除了记得Fedora 13虚拟机的登录信息,您还记得1999年你放弃的Angelfire上站点的账户和密码吗?当时,我们都在上面. 这是福尔摩斯称为填充的阁楼无用的信息.我们不能够清理东西从我们的阁楼,但我们可以信任的一

密码管理器

自媒体时代已经到来,导致各种重要的.不重要的账号密码搞得我都快记不过来了.经常记混,有时候需要试两三次,“忍无可忍,无需再忍.”so,今天我花了大半天的时间写了一个小程序(密码管理器),绝对的纯手打,整理文件整理到现在.我用它来帮我存储账号密码,本来只是想自己写个程序自己用,后来还是决定给大家分享一下.Ps:账号.密码都是经过加密存储的.我把程序发布到了CSDN( http://download.csdn.net/detail/vae_vae_vae/9269077 )和百度云(链接:  htt

PWDMan | Easysky 密码管理器

分类: 杂类工具    版本: 0.0.3    发布日期: 2015-08-10 一款简洁实用的密码管理器软件,便携.小巧.易用. 功能简介 支持多用户数据存储 支持数据分类及类别编辑 支持F4键及鼠标中键切换显示明文密码 支持复制指定项的全部数据以及特定数据 支持关键字搜索 支持手动锁定程序及窗口最小化时自动锁定程序 支持数据复制.剪切和粘贴操作 支持打开焦点行项目的位置(网址或文件路径) 支持导出为 .txt文本..csv 以及 .html 网页文件 支持导出数据时验证密码 支持从文本文件

安卓版的密码管理器

包含了账号密码的创建,类别的管理,备份和恢复,支持搜索,使用SQLite数据库,数据不会上传到服务器,本地使用,数据更加安全. 下载地址:密码管理器

怎么使用密码管理器如keepass更安全?

如今的网络世界,到处都需要密码,你肯定有一堆网络服务的密码资料:网络帐号.电子邮件.银行卡--这么多的帐号和密码,管理起来是个很麻烦的事情:记在脑子里大部分是做不到:记在纸上不安全,万一丢了就全没了:记在电子文档里相对安全(得加个密码),但总不方便,因为并不是随时都可以查看. KeePassPassword Safe(简称KeePass),是一款免费开源的密码管理软件,通过它,你只要记住一个主密码就可以管理你所有的网络帐号和密码了,并且KeePass会生成一个数据库文件,保存好这个数据库文件就等

C#WinForm treeview 简单文件夹管理器 查看文件夹下的文件,子文件下的文件

1 查看的文件夹中的内容 2 UI 3 代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 usi

Unity 简单的音效管理器

using UnityEngine; using System; using System.Collections.Generic; public enum _eSoundLayer { Background, Effect, EffectUI } public class SoundManager { SoundServer mSoundServer = new SoundServer(); Dictionary<_eSoundLayer, ISoundLayer> mSoundLayers