PDF passwordCracker
import PyPDF2
from itertools import product
import string
import time
def crack_pdf_bruteforce(pdf_path, max_length=4, charset=string.ascii_letters + string.digits):
start_time = time.time()
with open(pdf_path, 'rb') as f:
pdf_reader = PyPDF2.PdfReader(f)
if not pdf_reader.is_encrypted:
print("š PDF is not password protected.")
return
total_attempts = 0
for length in range(1, max_length + 1):
print(f"\nš Trying passwords of length {length}...")
for pwd_tuple in product(charset, repeat=length):
password = ''.join(pwd_tuple)
print(f"š Trying password: {password}", end='\r') # Overwrites the line for cleaner output
total_attempts += 1
try:
if pdf_reader.decrypt(password) == 1:
print(f"\nā
Password found: {password}")
print(f"š¢ Total attempts: {total_attempts}")
print(f"ā±ļø Time taken: {time.time() - start_time:.2f} seconds")
return
except Exception:
pass
print("\nā Password not found.")
print(f"š¢ Total attempts: {total_attempts}")
print(f"ā±ļø Time taken: {time.time() - start_time:.2f} seconds")
if __name__ == "__main__":
pdf_file = input("Enter path to encrypted PDF: ").strip().strip('"').strip("'")
crack_pdf_bruteforce(pdf_file, max_length=4)
Code output
š Trying passwords of length 1...
š Trying password: 9
š Trying passwords of length 2...
š Trying password: 99
š Trying passwords of length 3...