Hi, I’m Lex. I’m a bully. Lex 520 got an encrypted PDF from his ex-girlfriend, said he’d be surprised when he opened it. I cracked it in Python and found… 1, How to use Python to set a password for PDF. 2, How to use Python to decrypt encrypted PDF Collect collect!!
Here’s the thing
520 night, I’m having sex with my teammates
Suddenly, wechat’s profile picture of his ex-girlfriend jumped
Almost a year. Do you want to get back together?
It turned out to be an encrypted file called 520 Happy.pdf
Just say so if you want to get back together
Why do you have to have fun? Let me crack it
With my teammates screaming at me
I calmly and decisively quit the game
Out, my Python code…
Clear requirements
1. According to my understanding of my ex-girlfriend, the password is four pure digits. (You can customize the code generation function in the code to generate various combinations of passwords for cracking)
2, 520 happy. PDF ↓ ↓ ↓ Encrypted can not be opened
Install the PDF tool module
pip install PyPDF2
PS D:\> pip install PyPDF2 Looking in indexes: http://mirrors.aliyun.com/pypi/simple Collecting PyPDF2 Downloading http://mirrors.aliyun.com/pypi/packages/b4/01/68fcc0d43daf4c6bdbc6b33cc3f77bda531c86b174cac56ef0ffdb96faab/PyPDF2-1.26.0 . Tar. Gz (77 kB) | █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ | 77 kB, 919 kB/s Using legacy 'setup. Py install' for PyPDF2, since package 'wheel' is not installed. Installing collected packages: PyPDF2 Running setup.py install for PyPDF2 ... Done Successfully installed pypdf2-1.26.0 PS D:\>Copy the code
How to add password to PDF?
To crack encrypted PDF files, you need to know how to encrypt PDF files. PDF can be encrypted through the PyPDF2 module.
The code is as follows:
Import PyPDF2 # encrypt PDF def encrypt(old_Path, new_Path): """ :param old_Path: path name of the file to be encrypted :param new_Path: "" with open(old_Path, 'rb') as pdfFile: PdfWriter = pypdf2.pdffilereader () # For pageNum in range(pdfreader.numPages): Pdfwriter.addpage (pdfReader.getPage(pageNum)) # Set password to 8888 PDfwriter.encrypt ('8888') with open(new_Path, 'wb') as resultPDF: pdfWriter. Write (resultPDF) print(' Encrypt successfully! ')Copy the code
How to crack encrypted PDF files
1. Method of generating four-digit pure digit cipher
You can specify the number of digits of the password as required. Only four digits are defined here
PWD = STR (I). Zfill (4) print(PWD)Copy the code
2. Crack the PDF function code
Referring to the pypdf2 module, we call pdfReader.decrypt(‘ password ‘) by repeatedly iterating over the generated password.
Password cracking functions are as follows:
Def decrypt(old_Path, new_Path): """ :param old_Path: specifies the path name of the file to be encrypted. "" with open(old_Path, 'rb') as pdfFile: PdfReader = pypdf2.pdffilereader (pdfFile) pdfWriter = pypdf2.pdffilewriter () # determine whether the file isEncrypted if pdfReader. IsEncrypted: PWD = STR (I).zfill(4) if pdfreader.decrypt (PWD): for pageNum in range(pdfReader.numPages): pdfWriter.addPage(pdfReader.getPage(pageNum)) with open(new_Path, 'wb') as resultFile: Pdfwriter. write(resultFile) print(' Successful! '+ PWD 'else: print(' wrong password! Else: print(' no encryption ~~~')Copy the code
Start to crack
Now that the code is ready, let’s start cracking it
The effect is as follows ↓ ↓ ↓
A few seconds later, the password was cracked.
Emmm. The password is 1314
The complete code
From OS import error import PyPDF2 # encrypt PDF def encrypt(old_Path, new_Path): """ :param old_Path: Path of the file to be encrypted :param new_Path: path of the encrypted file """ with open(old_Path, 'rb') as pdfFile: PdfWriter = pypdf2.pdffilereader () # For pageNum in range(pdfreader.numPages): Pdfwriter.addpage (pdfReader.getPage(pageNum)) # Set password to 8888 PDfwriter.encrypt ('8888') with open(new_Path, 'wb') as resultPDF: pdfWriter. Write (resultPDF) print(' Encrypt successfully! , ') def decrypt(old_Path): """ :param old_Path: path name of the file to be encrypted :param new_Path: "" with open(old_Path, 'rb') as pdfFile: PdfReader = pypdf2. PdfFileReader(pdfFile) # determine whether the file isEncrypted if pdfReader. IsEncrypted: # determine whether the password is correct for I in range(10000): PWD = STR (I).zfill(4).replace(',') print(PWD) try: PDfreader.decrypt (PWD) except: print(' decrypt ') else: Print (' Success! The password is: '+ PWD) break the else: print (" oh ~ no code) if __name__ = =' __main__ ': #encrypt('E:/520 happy.pdf ','E:/520 happy.pdf ') #encrypt('E:/520 happy.pdf ','E:/520 happy.pdf ') # Encrypt ('E:/520 happy.pdf ') #encrypt('E:/520 happy.pdf ')Copy the code
The story ends
The password was 1314
I’m a little overwhelmed
Can’t wait to open “520 happy.pdf”
Spluttered pa
Cheerful input cracked out of the password 1314
—The End—-
Reproduced in this article. If there is infringement contact xiaobian delete!
Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base