?本文在CSDN"彭_Yu的博客"同步發表
目錄
1.要點
2.運行原理
3.異或演算法簡介
4.運行效果
5.實作程序
5.1檔案結構
5.2建立資料庫
5.3 Python代碼
?
注:程式實體可到文末下載
1.要點
1.tkinter界面設計
2.SQLite資料庫操作
3.字串異或運算加密和解密
2.運行原理
1.用戶需要記住一個統一的加解密密鑰,對于各平臺的密碼,使用密鑰字串異或運算加密后存盤到資料庫,查詢時使用同一個密鑰進行密鑰字串異或解密,
2.需要注意的是,由于代碼采用的是異或演算法,所以密碼字串和密鑰字串不應有對應位置上相同的字符,
3.由于代碼采用的是異或演算法所以并不安全,他人猜到的加解密密鑰與正確密鑰越相似,解密出的密碼也就與正確密碼越相似,你可以改寫加密和解密演算法,實作更高級別的密碼保護,
3.異或演算法簡介
XOR 是 exclusive OR 的縮寫,英語的 exclusive 意思是"專有的,獨有的",可以理解為 XOR 是更單純的 OR 運算,
我們知道,OR 運算的運算子有兩種情況,計算結果為
true,(1)一個為 true,另一個為 false; (2)兩個都為 true,![]()
上面兩種情況,有時候需要明確區分,所以引入了 XOR,
XOR 排除了第二種情況,只有第一種情況(一個運算子為
true,另一個為false)才會回傳 true,所以可以看成是更單純的 OR 運算,也就是說, XOR 主要用來判斷兩個值是否不同,XOR 一般使用插入符號(caret)
^表示,如果約定0為 false,1為 true,那么 XOR 的運算真值表如下,0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0![]()
4.運行效果
?
5.實作程序
5.1檔案結構
/根目錄
-MyPWD.exe(主程式)
-MyPWD.sqlite3(資料庫檔案)
5.2建立資料庫
在這里我們可以使用在線sqlite查看器:
在線sqlite查看器
輸入如下資訊:
CREATE TABLE passwords (platform TEXT, pwd TEXT, id INTEGER PRIMARY KEY)
?
單擊執行 sql>匯出Sqlite資料庫檔案 并將檔案重命名為 “MyPWD.sqlite3” 放入MyPWD.exe(主程式)所在目錄,
?
?
5.3 Python代碼
import sqlite3
import tkinter
from itertools import cycle
from tkinter.ttk import Combobox
from tkinter.messagebox import showinfo, showerror, askyesno
class DatabaseAccess:
@staticmethod
def doSql(sql):
with sqlite3.connect('MyPWD.sqlite3') as conn:
conn.execute(sql)
conn.commit()
@staticmethod
def getData(sql):
with sqlite3.connect('MyPWD.sqlite3') as conn:
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
root = tkinter.Tk()
root.geometry('350x250+400+300')
root.resizable(False, False)
root.title('(C)2022彭_Yu')
lbKey = tkinter.Label(root, text='密碼資料庫密鑰:')
lbKey.place(x=10, y=10, width=100, height=20)
key = tkinter.StringVar(root, '')
entryKey = tkinter.Entry(root, textvariable=key, show='*')
entryKey.place(x=120, y=10, width=200, height=20)
lbPlatform = tkinter.Label(root, text='平臺 名稱:')
lbPlatform.place(x=10, y=40, width=100, height=20)
platformName = tkinter.StringVar(root, '')
entryPlatform = tkinter.Entry(root, textvariable=platformName)
entryPlatform.place(x=120, y=40, width=200, height=20)
lbPassword = tkinter.Label(root, text='設定 密碼:')
lbPassword.place(x=10, y=70, width=100, height=20)
password = tkinter.StringVar(root, '')
entryPassword = tkinter.Entry(root, textvariable=password)
entryPassword.place(x=120, y=70, width=200, height=20)
def add_modify():
if not (key.get() and platformName.get() and password.get()):
showerror('出錯',
'請同時輸入密碼資料庫密鑰、平臺名稱、密碼.\n注意:密鑰不要隨意更改.')
return
if key.get().isdigit():
showerror('密鑰安全性出錯', '為了您的密鑰安全,不能使用純數字作為密鑰')
return
if sum(map(lambda x,y: x==y, password.get(), key.get())) > 0:
showerror('密鑰安全性出錯', '密碼不合適,為了您的密鑰安全,密碼和密鑰不能有對應位置相同的字符')
return
pwd = ''.join(map(lambda x,y: chr(ord(x)^ord(y)), password.get(), cycle(key.get())))
sql = 'SELECT * FROM passwords WHERE platform="'+platformName.get()+'"'
if len(DatabaseAccess.getData(sql)) == 1:
sql = 'UPDATE passwords SET pwd="'+pwd+'" WHERE platform="'+platformName.get()+'"'
DatabaseAccess.doSql(sql)
showinfo('恭喜請求執行成功', '修改密碼成功')
else:
sql = 'INSERT INTO passwords(platform,pwd) VALUES("'+platformName.get()+'","'+pwd+'")'
DatabaseAccess.doSql(sql)
bindPlatformNames()
showinfo('恭喜請求執行成功', '增加密碼成功')
btnAddModify = tkinter.Button(root,
text='增加或修改密碼',
bg='cyan',
fg='black',
command=add_modify)
btnAddModify.place(x=20, y=100, width=300, height=20)
lbChoosePlatform = tkinter.Label(root, text='請選擇平臺:')
lbChoosePlatform.place(x=10, y=130, width=100, height=20)
def bindPlatformNames():
sql = 'SELECT platform FROM passwords'
data = https://www.cnblogs.com/pyublog/p/DatabaseAccess.getData(sql)
data = [item[0] for item in data]
comboPlatform['values'] = data
comboPlatform = Combobox(root)
bindPlatformNames()
comboPlatform.place(x=120, y=130, width=200, height=20)
lbResult = tkinter.Label(root, text='查詢 結果:')
lbResult.place(x=10, y=160, width=100, height=20)
result = tkinter.StringVar(root, '')
entryResult = tkinter.Entry(root, textvariable=result)
entryResult['state'] = 'disabled'
entryResult.place(x=120, y=160, width=200,height=20)
def getPassword():
if not comboPlatform.get().strip():
showerror('出錯', '還沒選擇平臺名稱')
return
if not key.get():
showerror('出錯', '請輸入密鑰')
return
sql = 'SELECT pwd FROM passwords WHERE platform="'+comboPlatform.get()+'"'
pwd = DatabaseAccess.getData(sql)[0][0]
pwd = ''.join(map(lambda x,y: chr(ord(x)^ord(y)), pwd, cycle(key.get())))
result.set(pwd)
btnGetResult = tkinter.Button(root,
text='查詢密碼',
bg='cyan',
fg='black',
command=getPassword)
btnGetResult.place(x=20, y=190, width=149, height=20)
def deletePassword():
if not comboPlatform.get().strip():
showerror('出錯', '您還沒選擇平臺名稱')
return
if not askyesno('請確認您的請求', '確定要洗掉嗎?洗掉后不可恢復!'):
return
sql = 'DELETE FROM passwords WHERE platform="'+comboPlatform.get()+'"'
DatabaseAccess.doSql(sql)
showinfo('恭喜操作成功完成', '密碼洗掉成功')
bindPlatformNames()
btnDelete = tkinter.Button(root, text='洗掉密碼',
bg='red', fg='yellow',
command=deletePassword)
btnDelete.place(x=179, y=190, width=140, height=20)
root.mainloop()
然后將此程式編譯為exe,當然不編譯也可以但要保證 MyPWD.py 檔案與 MyPWD.sqlite3 資料庫檔案在同一目錄下,
關于如何編譯請查看我以前的一篇文章,做好準備操作但不要執行其中的編譯命令,而是執行以下命令(執行前請保證目錄結構與以下圖片對應),
#Python #日常技巧 #功能 將Python檔案編譯或打包成可執行(EXE)檔案_python編譯成可執行檔案_彭_Yu的博客-CSDN博客#Python #日常技巧 #功能 將Python檔案編譯或打包成可執行(EXE)檔案,將Python檔案編譯為exe檔案后,可以直接在Windows上運行,不需要再依賴Python環境,可以復制到其他電腦中直接使用,較為方便,
https://blog.csdn.net/2201_75480799/article/details/128058849
做好準備操作后,執行如下代碼:
cd X:\源代碼
pyinstaller -F -w -i X:\源代碼\icon.ico X:\源代碼\MyPWD.py
?
出現如下字符說明編譯成功,請將根目錄中 dist 檔案夾中的MyPWD.exe(主程式)放入 “MyPWD.sqlite3” 所在目錄,
?
預祝使用順利~
文章資源下載:
https://pan.baidu.com/s/1cW8kRcQFOF2tBBj05UZmZg
提取碼:2xr7
感謝您的閱讀,如覺得有用請您點贊,您的鼓勵是對我的最大動力!
END
2022/12/24
?
本文作者:彭_Yu
轉載請先聯系[email protected]并注明原文鏈接:https://www.cnblogs.com/pyublog/p/17105402.html否則為侵權行為!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543388.html
標籤:Python
下一篇:Python裝飾器實體講解(二)
