Python PDF Processing: Crack Encrypted PDF File Using Password Dictionary

In this tutorial, we will use some steps to introduce how to crack encrypted pdf file using a password dictionary in python.

Python PDF Processing: Crack Encrypted PDF File Using Password Dictionary

1.Install pikepdf

pip install pikepdf

pikepdf can allow us to open pdf with password.

2.Import library

import pikepdf

3.Try to use password to open an encrypted pdf file

passwords_filename = "passwords_list.txt"
locked_pdf_file = "my_locked.pdf"

#load passwords file
with open(passwords_filename) as file:
    
    passwords_list = file.readlines()
    total_passwords = len(passwords_list)

    for index,password in enumerate(passwords_list):
        
        #try if password is correct
        try:
            with pikepdf.open(locked_pdf_file, password = password.strip()) as pdf_file:
                print("\n++++++++++++++++++++++SUCCESS+++++++++++++++")
                print("Success---------- File is Unlocked and the password is: ", password)
                break
        #if password fail
        except:
            print("\n=====================")
            print(f"Trying Password {password} --- Fail!!!!")
            scanning =  (index/total_passwords)*100
            print("Scanning passwords complete:", round(scanning, 2))
            continue

In this code, we have done two things:

(1) Read password line by line in password dictionary, the code is:

passwords_list = file.readlines()
for index,password in enumerate(passwords_list):

(2) Try to use a password to open pdf file, the code is:

with pikepdf.open(locked_pdf_file, password = password.strip()) as pdf_file:

If the password is correct, the pdf file will be opened successfully.