-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (70 loc) · 2.46 KB
/
main.py
File metadata and controls
93 lines (70 loc) · 2.46 KB
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
from pathlib import Path
from PyPDF2 import PdfReader, PdfWriter, errors
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox
import ttkbootstrap as tb
from ttkbootstrap import Querybox
from ttkbootstrap.dialogs import Messagebox
def encrypt(file_path,password):
file = Path(file_path)
if not file.exists():
print('[!] File not exist in given path. Try again')
return
new_file_name = f"encrypted_{file.name}"
reader = PdfReader(file_path)
writer = PdfWriter()
for page in range(len(reader.pages)):
writer.add_page(reader.pages[page])
writer.encrypt(password)
print(f"[*] Writing to {new_file_name}...")
with open(new_file_name,'wb') as file:
writer.write(file)
print(f"[+] Created {new_file_name}")
Messagebox.ok("File encrypted successfully!", title="Success", alert=True)
def decrypt():
file_path = input("[+] Enter file path: ")
file = Path(file_path)
if not file.exists():
print("[!] File not exists in given path. Try again")
return
password = input("[+] Enter password for decryption: ")
new_file_name = input("[+] Enter new file name: ")
reader = PdfReader(file_path)
writer = PdfWriter()
try:
result = reader.decrypt(password)
if result == 0:
print("[!] Incorrect password.")
return
except errors.FileNotDecryptedError:
print("[!] Error decrypting the file.")
return
for page in range(len(reader.pages)):
writer.add_page(reader.pages[page])
print(f"[*] Writing to {new_file_name}...")
with open(new_file_name,'wb') as file:
writer.write(file)
print(f"[+] Created {new_file_name}")
messagebox.showinfo("Success", f"File: {file_path}\nPassword: {password}", parent=window)
def select_file():
file_path = filedialog.askopenfilename(filetypes=[('PDF','.pdf')])
if file_path:
password = Querybox.get_string("Enter password for encryption", title="Password")
if password:
encrypt(file_path, password)
def main():
window.title("Encrypt && Decrypt PDF")
window.geometry('400x200')
btn = tb.Button(
window,
text="Select File to Encrypt",
bootstyle="primary", # Correct bootstyle usage
width=30,
command=select_file,
padding=(10, 15),
)
btn.pack(expand=True)
window.mainloop()
window = tk.Tk()
if __name__ == "__main__":
main()