"Python Atomation : Crack 4 digit numeric Pdf password using sequence and random methods"
- Anu Solanki
- Dec 24, 2023
- 2 min read

install and import pikepdf and tqdm libraries:
pip install pikepdf tqdm
import pikepdf
import tqdm
from datetime import datetime
import time
Sequence Method :
Declare important variable
must be change the variable values according to your need
start_time = datetime.now()
# Set the range for passwords
password_range = (1000, 9999+1)
# Specify the PDF file path
pdf_file_path = "PDF-protected.pdf"
For loop that is starting from 1000 to 10000 check password inbetween those number
also write a right password in file
for password_to_find in tqdm(range(password_range[0], password_range[1] + 1), desc="Decrypting PDF", unit="password"):
try:
# open PDF file
with pikepdf.open(pdf_file_path, password=str(password_to_find)) as pdf:
# Password decrypted successfully, break out of the loop
print("[+] Password found:", password_to_find)
with open('ps_found_sequence.txt', 'w') as file:
file.write(str(password_to_find))
break
except pikepdf._core.PasswordError as e:
# wrong password, just continue in the loop
print("[-] Incorrect password:", password_to_find)
continue
end_time = datetime.now()
# Calculate the elapsed time
elapsed_time = end_time - start_time
# Print the start and end times, as well as the elapsed time
print("Start Time:", start_time.strftime("%H:%M:%S"))
print("End Time:", end_time.strftime("%H:%M:%S"))
print("Elapsed Time:", str(elapsed_time))
Random Method :
Declare important variable
must be change the variable values according to your need
start_time = datetime.now()
# Set the range for random passwords
password_range = (1000, 9999+1)
# Specify the PDF file path
pdf_file_path = "PDF-protected.pdf"
For loop that is starting from 1000 to 10000 and check random password inbetween those number
also write a right password in file
while True:
# Generate a random password within the specified range
password_to_find = random.randint(*password_range)
# Use tqdm with a range of 1 to show progress (1 iteration for trying one password)
for _ in tqdm(range(1), desc="Decrypting PDF", unit="password"):
try:
# open PDF file
with pikepdf.open(pdf_file_path, password=str(password_to_find)) as pdf:
# Password decrypted successfully, break out of the loop
print("[+] Password found:", password_to_find)
with open('pssp_found.txt', 'w') as file:
file.write(str(password_to_find))
break
except pikepdf._core.PasswordError as e:
# wrong password, just continue in the loop
print("[-] Incorrect password:", password_to_find)
continue
end_time = datetime.now()
# Calculate the elapsed time
elapsed_time = end_time - start_time
# Print the start and end times, as well as the elapsed time
print("Start Time:", start_time.strftime("%H:%M:%S"))
print("End Time:", end_time.strftime("%H:%M:%S"))
print("Elapsed Time:", str(elapsed_time))
Comments