...

Opening password protected pdf file using Python.

How many times does it happens with us that we use our professional skills to solve daily life problems. For software developers, it would hardly happen that they write a piece of code for themself. Recently it happened to me. Not sure how but one of my personal bank accounts had the wrong personal details. They sent me statement in a password-protected file, but when I was trying to open the file it prompted "Incorrect Password". I was urgent and I had to view the information in that file. Cutting long story short, I decided to crack the password of the file. 

So the password was, first four letters of my name followed by the date of birth. 

  1. First four characters of name .
  2. date of birth DDMM format. 

Since in other communication, they had my correct name. So I knew I don't have to work on the first condition.It has to be the second condition which I have to crack. 

So wrote a Python program to loop through all the dates of the year and try to open the file. Its a tedious and boring task if you do it manually but for computers it's a piece of cake. 

so here goes the program for you guys. 

 
from datetime import timedelta, date
import pikepdf
import datetime
dt  = datetime.date(2015, 1, 20)
print(dt.day)

start_date = date(2021, 1, 1)
end_date = date(2022, 1, 1)
def daterange(start_date, end_date):
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)
        
def open_pdf(f,msg):
    with pikepdf.open(f,password=msg) as pdf:
        num_pages = len(pdf.pages)
        print("Total pages:", num_pages)
        
for single_date in daterange(start_date, end_date):
    msg = "Name"+single_date.strftime("%d%m")

    try:
        open_pdf("icici.pdf",msg)
        print(msg)
    except:
        print(end='')

and finally above program will print the password which opened the file. the fun part is you can crack any password if you have a hint of the password. 

So the learning is, Keep your password as strong as possible so that it cannot be broken easily. 

The purpose of this article is to share the use case of programming in daily life. 

Have fun!

 

Post Comments(0)

Login to comment