1140116-
使用繁體中文,在w11下,使用python語言及UI界面,要如何製作"資料夾鎖",讓使用者方便將資料夾上鎖及解鎖?
1140116-folderlock-05.py
-資料夾下的zip等無法被加密
-密碼要輸入兩次
-有後門密碼
import os
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import hashlib # 使用標準庫的 scrypt
# 全局變數
password = None
password_hint = None # 密碼提示
lock_status = False
BACKDOOR_PASSWORD = "admin123" # 後門密碼
# 加密與解密
def encrypt_folder(folder_path, password):
try:
# 用密碼生成密鑰
salt = os.urandom(16)
key = hashlib.scrypt(password.encode(), salt=salt, n=2**14, r=8, p=1, maxmem=0, dklen=32)
cipher = AES.new(key, AES.MODE_CBC)
# 遍歷資料夾,對每個文件加密
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
file_data = f.read()
padded_data = pad(file_data, AES.block_size)
encrypted_data = cipher.encrypt(padded_data)
# 儲存加密過的資料
with open(file_path + ".enc", 'wb') as f:
f.write(salt + cipher.iv + encrypted_data)
os.remove(file_path) # 刪除原始文件
return True
except Exception as e:
messagebox.showerror("錯誤", f"加密過程出現錯誤:{str(e)}")
return False
def decrypt_folder(folder_path, password):
try:
# 遍歷資料夾,對每個加密文件解密
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".enc"):
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
enc_data = f.read()
salt = enc_data[:16]
iv = enc_data[16:32]
encrypted_data = enc_data[32:]
# 生成密鑰並解密
key = hashlib.scrypt(password.encode(), salt=salt, n=2**14, r=8, p=1, maxmem=0, dklen=32)
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)
# 儲存解密後的資料
with open(file_path[:-4], 'wb') as f:
f.write(decrypted_data)
os.remove(file_path) # 刪除加密文件
return True
except Exception as e:
messagebox.showerror("錯誤", f"解密過程出現錯誤:{str(e)}")
return False
# UI介面功能
def lock_folder():
global password, password_hint, lock_status
folder = filedialog.askdirectory(title="選擇資料夾")
if folder:
# 提醒用戶備份
messagebox.showinfo("提醒", "請確認您已備份原始檔案,否則密碼遺失將無法恢復資料!")
# 設定密碼與提示
while True:
password = simpledialog.askstring("設定密碼", "請設定資料夾密碼:", show='*')
if not password:
messagebox.showerror("錯誤", "密碼不可為空!")
return
confirm_password = simpledialog.askstring("確認密碼", "請再次輸入資料夾密碼:", show='*')
if password != confirm_password:
messagebox.showerror("錯誤", "兩次輸入的密碼不一致,請重新輸入!")
else:
break
password_hint = simpledialog.askstring("設定提示", "請輸入密碼提示(可選):")
if encrypt_folder(folder, password):
lock_status = True
messagebox.showinfo("成功", "資料夾已鎖定!")
def unlock_folder():
global password, password_hint, lock_status
folder = filedialog.askdirectory(title="選擇資料夾")
if folder:
# 顯示密碼提示
if password_hint:
hint_message = f"提示:{password_hint}"
messagebox.showinfo("密碼提示", hint_message)
# 輸入密碼並檢查後門密碼
input_password = simpledialog.askstring("輸入密碼", "請輸入資料夾密碼:", show='*')
if input_password == password or input_password == BACKDOOR_PASSWORD:
if decrypt_folder(folder, password):
lock_status = False
messagebox.showinfo("成功", "資料夾已解鎖!")
else:
messagebox.showerror("錯誤", "密碼錯誤!")
# 主視窗
root = tk.Tk()
root.title("資料夾鎖")
root.geometry("300x200")
lock_button = tk.Button(root, text="鎖定資料夾", command=lock_folder)
lock_button.pack(pady=20)
unlock_button = tk.Button(root, text="解鎖資料夾", command=unlock_folder)
unlock_button.pack(pady=20)
root.mainloop()
1140116-folderlock-04.py
-資料夾下的zip等無法被加密
-有後門
import os
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import hashlib # 使用標準庫的 scrypt
# 全局變數
password = None
password_hint = None # 密碼提示
lock_status = False
BACKDOOR_PASSWORD = "admin123" # 後門密碼
# 加密與解密
def encrypt_folder(folder_path, password):
try:
# 用密碼生成密鑰
salt = os.urandom(16)
key = hashlib.scrypt(password.encode(), salt=salt, n=2**14, r=8, p=1, maxmem=0, dklen=32)
cipher = AES.new(key, AES.MODE_CBC)
# 遍歷資料夾,對每個文件加密
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
file_data = f.read()
padded_data = pad(file_data, AES.block_size)
encrypted_data = cipher.encrypt(padded_data)
# 儲存加密過的資料
with open(file_path + ".enc", 'wb') as f:
f.write(salt + cipher.iv + encrypted_data)
os.remove(file_path) # 刪除原始文件
return True
except Exception as e:
messagebox.showerror("錯誤", f"加密過程出現錯誤:{str(e)}")
return False
def decrypt_folder(folder_path, password):
try:
# 遍歷資料夾,對每個加密文件解密
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".enc"):
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
enc_data = f.read()
salt = enc_data[:16]
iv = enc_data[16:32]
encrypted_data = enc_data[32:]
# 生成密鑰並解密
key = hashlib.scrypt(password.encode(), salt=salt, n=2**14, r=8, p=1, maxmem=0, dklen=32)
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)
# 儲存解密後的資料
with open(file_path[:-4], 'wb') as f:
f.write(decrypted_data)
os.remove(file_path) # 刪除加密文件
return True
except Exception as e:
messagebox.showerror("錯誤", f"解密過程出現錯誤:{str(e)}")
return False
# UI介面功能
def lock_folder():
global password, password_hint, lock_status
folder = filedialog.askdirectory(title="選擇資料夾")
if folder:
# 提醒用戶備份
messagebox.showinfo("提醒", "請確認您已備份原始檔案,否則密碼遺失將無法恢復資料!")
# 設定密碼與提示
password = simpledialog.askstring("設定密碼", "請設定資料夾密碼:", show='*')
if password:
password_hint = simpledialog.askstring("設定提示", "請輸入密碼提示(可選):")
if encrypt_folder(folder, password):
lock_status = True
messagebox.showinfo("成功", "資料夾已鎖定!")
def unlock_folder():
global password, password_hint, lock_status
folder = filedialog.askdirectory(title="選擇資料夾")
if folder:
# 顯示密碼提示
if password_hint:
hint_message = f"提示:{password_hint}"
messagebox.showinfo("密碼提示", hint_message)
# 輸入密碼並檢查後門密碼
input_password = simpledialog.askstring("輸入密碼", "請輸入資料夾密碼:", show='*')
if input_password == password or input_password == BACKDOOR_PASSWORD:
if decrypt_folder(folder, password):
lock_status = False
messagebox.showinfo("成功", "資料夾已解鎖!")
else:
messagebox.showerror("錯誤", "密碼錯誤!")
# 主視窗
root = tk.Tk()
root.title("資料夾鎖")
root.geometry("300x200")
lock_button = tk.Button(root, text="鎖定資料夾", command=lock_folder)
lock_button.pack(pady=20)
unlock_button = tk.Button(root, text="解鎖資料夾", command=unlock_folder)
unlock_button.pack(pady=20)
root.mainloop()
要在Windows 11下使用Python語言及UI界面來製作一個"資料夾鎖"工具,主要目標是讓使用者能夠簡單地將資料夾進行鎖定與解鎖操作。以下是設計這個工具的基本步驟:
tkinter 是 Python 的標準 GUI 库,可以用來建立簡單的圖形用戶界面。可以使用它來設計一個簡單的界面,讓使用者選擇要鎖定或解鎖的資料夾。
對於"鎖定"資料夾的方式,可以使用密碼來加密資料夾的內容,或者使用Windows系統本身的ACL(訪問控制清單)來管理資料夾的訪問權限。
這裡我們使用密碼保護來進行鎖定。鎖定時,我們將資料夾內的內容加密,解鎖時需要輸入正確的密碼才能查看資料夾。
Python 3.x
tkinter(用來建立UI介面)
pycryptodome(加密用的第三方庫,進行資料夾加密)
安裝依賴
bash
複製
pip install pycryptodome
編寫Python程式
python
複製
import os
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox # 加上 simpledialog
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
from Crypto.Util.Padding import pad, unpad
import base64
# 全局變數
password = None
lock_status = False
# 加密與解密
def encrypt_folder(folder_path, password):
try:
# 用密碼生成密鑰
salt = os.urandom(16)
key = scrypt(password.encode(), salt, dklen=32, N=2**14, r=8, p=1)
cipher = AES.new(key, AES.MODE_CBC)
# 遍歷資料夾,對每個文件加密
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
file_data = f.read()
padded_data = pad(file_data, AES.block_size)
encrypted_data = cipher.encrypt(padded_data)
# 儲存加密過的資料
with open(file_path + ".enc", 'wb') as f:
f.write(salt + cipher.iv + encrypted_data)
os.remove(file_path) # 刪除原始文件
return True
except Exception as e:
messagebox.showerror("錯誤", f"加密過程出現錯誤:{str(e)}")
return False
def decrypt_folder(folder_path, password):
try:
key = scrypt(password.encode(), salt, dklen=32, N=2**14, r=8, p=1)
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".enc"):
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
enc_data = f.read()
salt = enc_data[:16]
iv = enc_data[16:32]
encrypted_data = enc_data[32:]
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)
# 儲存解密後的資料
with open(file_path[:-4], 'wb') as f:
f.write(decrypted_data)
os.remove(file_path) # 刪除加密文件
return True
except Exception as e:
messagebox.showerror("錯誤", f"解密過程出現錯誤:{str(e)}")
return False
# UI介面
def lock_folder():
global password, lock_status
folder = filedialog.askdirectory(title="選擇資料夾")
if folder:
password = simpledialog.askstring("設定密碼", "請設定資料夾密碼:", show='*')
if encrypt_folder(folder, password):
lock_status = True
messagebox.showinfo("成功", "資料夾已鎖定!")
def unlock_folder():
global password, lock_status
folder = filedialog.askdirectory(title="選擇資料夾")
if folder:
input_password = simpledialog.askstring("輸入密碼", "請輸入資料夾密碼:", show='*')
if input_password == password:
if decrypt_folder(folder, password):
lock_status = False
messagebox.showinfo("成功", "資料夾已解鎖!")
else:
messagebox.showerror("錯誤", "密碼錯誤!")
# 主視窗
root = tk.Tk()
root.title("資料夾鎖")
root.geometry("300x150")
lock_button = tk.Button(root, text="鎖定資料夾", command=lock_folder)
lock_button.pack(pady=20)
unlock_button = tk.Button(root, text="解鎖資料夾", command=unlock_folder)
unlock_button.pack(pady=20)
root.mainloop()
encrypt_folder():加密資料夾中的檔案。
decrypt_folder():解密資料夾中的檔案。
lock_folder():使用者選擇要鎖定的資料夾,並設置密碼來加密該資料夾中的檔案。
unlock_folder():使用者選擇要解鎖的資料夾,並輸入密碼進行解密。
執行程式後,會開啟一個視窗。
點擊"鎖定資料夾",選擇一個資料夾並設置密碼,該資料夾中的檔案會被加密。
點擊"解鎖資料夾",選擇已鎖定的資料夾並輸入密碼,檔案會被解密並還原。
程式中使用了對資料夾中的每個檔案進行加密。這樣做的好處是能夠保證檔案在未解密的情況下無法查看。
加密過程會刪除原始檔案,並生成加密過的檔案,解密後會將加密檔案還原。
需要確保密碼的管理安全,不應該隨意丟失或洩漏。
這樣,你就能夠簡單地在 Windows 11 上使用 Python 製作一個基本的資料夾鎖工具,讓使用者能夠鎖定和解鎖資料夾。