-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
35 lines (28 loc) · 1.05 KB
/
Copy pathmain.py
File metadata and controls
35 lines (28 loc) · 1.05 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
from PyPDF2 import PdfReader, PdfWriter
def brute_force_pdf_password(reader: PdfReader, secure_pii: bool) -> str:
try_password = 0
while reader.is_encrypted:
try_password += 1
password = str(try_password).zfill(6)
try:
reader.decrypt(password)
len(reader.pages)
protected_password = password if not secure_pii else "****"
print(f"Password found: {protected_password}")
return password
except Exception as e:
if try_password % 10000 == 0:
print(f"Trying password: {password}")
continue
return ""
def decrypt_pdf(input_path: str, output_path: str, secure_pii: bool) -> None:
reader = PdfReader(input_path)
if reader.is_encrypted:
brute_force_pdf_password(reader, secure_pii)
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open(output_path, "wb") as f:
writer.write(f)
if __name__ == "__main__":
decrypt_pdf("Fatura.pdf", "decrypted-pdf.pdf", True)