The encrypted "520 Happy.pdf" sent by my ex-girlfriend, I cracked it with python, but found out. . .

520 received an encrypted PDF file from his ex-girlfriend, saying that there was a surprise after opening it. Could it be that they want to get back together?
After I cracked it with python, I found out. . .
The key points of this article:
1. How to use python to set a password for pdf
2. How to use python to crack encrypted pdf

it's like this

In the evening of 520, I was having sex with my teammates

Suddenly, the profile picture of my ex-girlfriend jumped up on WeChat

It's been almost a year, do you want to get back together?

What I sent was an encrypted file of "520 Happy.pdf"

Just say it if you want to get back together

Why do you have to be so interesting, let me crack it

Accompanied by the harsh curses of my teammates

I quit the game calmly and decisively

Rolled out, my python code. . .

clear needs

1. According to the understanding of the ex-girlfriend, the password is 4 pure numbers. (You can customize the code generation function in the code to generate various combinations of passwords for cracking)

2. 520 Happy.pdf is as follows ↓ ↓ ↓ It is encrypted and cannot be opened

Install the pdftools 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:\>

How to add password to pdf?

If you want to crack encrypted pdf files, you need to know how to encrypt pdf. The pdf can be encrypted through the PyPDF2 module.

code show as below:

import PyPDF2
#加密PDF
def encrypt(old_Path, new_Path):
    """
    :param old_Path: 待加密文件的路径名
    :param new_Path: 加密之后的文件路径名
    """
    with open(old_Path, 'rb') as pdfFile: 
        pdfReader = PyPDF2.PdfFileReader(pdfFile)
        # 创建pdfWriter对象用于写出PDF文件
        pdfWriter = PyPDF2.PdfFileWriter()
        # pdf对象加入到pdfWriter对象中
        for pageNum in range(pdfReader.numPages):
            pdfWriter.addPage(pdfReader.getPage(pageNum))
        # 密码设置为8888
        pdfWriter.encrypt('8888')
        with open(new_Path, 'wb') as resultPDF:
            pdfWriter.write(resultPDF)
            print('加密成功!')

How to crack encrypted pdf files

1. The method of generating a four-digit pure digital password

You can define the number of digits of the password according to your needs. Here, only 4 digits of pure numeric passwords are defined.

#你可以根据需求,自己定义密码的位数,这里只定义4位纯数字密码
for i in range(10000):
	#生成四位数密码
	pwd=str(i).zfill(4)
	print(pwd)

2. Crack the pdf function code

Refer to the pypdf2 module, call pdfReader.decrypt('password'), and traverse the password we generated continuously.

The function to crack the password is as follows:

def decrypt(old_Path, new_Path):
    """
    :param old_Path: 待加密文件的路径名
    :param new_Path: 加密之后的文件路径名
    """
    with open(old_Path, 'rb') as pdfFile:
        pdfReader = PyPDF2.PdfFileReader(pdfFile)
        pdfWriter = PyPDF2.PdfFileWriter()
        # 判断文件是否加密
        if pdfReader.isEncrypted:
            # 判断密码是否正确
            for i in range(10000):
                #生成四位数密码
                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('成功了!密码是:'+pwd)
            else:
                print('密码错了!哼~~~')
        else:
            print('没有加密呀~~~')

start cracking

The code is ready, below, we officially start to crack~~~

The effect is as follows ↓ ↓ ↓

After a few seconds, the password was cracked successfully.

emmm, the password is actually 1314

The complete code is collected at the business card at the end of the article

the end of the story

The password is actually 1314

I'm a little overwhelmed

Can't wait to open "520 Happy.pdf"

clap clap

Cheerfully enter the cracked password 1314

Guess you like

Origin blog.csdn.net/fei347795790/article/details/129570804