520晚上,我用python破解了前女友发来的加密pdf文件,结果却发现...

17,736 阅读4分钟

事情是这样的

520晚上,正跟队友 啪啪啪 组团开黑

突然,微信上前女友的头像跳动了起来

快一年了,难道是想要复合?

发来的竟是一个 " 520快乐.pdf " 的加密文件

想复合就直说嘛

干嘛还要搞的这么有情趣,让我破解

伴随着我队友刺耳的骂街声

我平静而果断的的退出了游戏

撸出了,我的python代码。。。

明确需求

1、根据对前女友的了解,密码为4位纯数字。(代码中可以自定义代码生成函数,生成各种组合的密码,进行破解)

2、520快乐.pdf 如下 ↓ ↓ ↓ 加密了打不开

安装pdf工具模块

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:\>

如何给pdf加密码?

要想破解加密的pdf文件,就要知道如何给pdf加密。可以通过PyPDF2模块,给pdf加密。

代码如下:

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('加密成功!')

如何破解加密pdf文件

1、生成四位数纯数字密码的方法

你可以根据需求,自己定义密码的位数,这里只定义4位纯数字密码

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

2、破解pdf函数代码

引用pypdf2模块,调用pdfReader.decrypt('密码'),通过不停的遍历我们生成的密码。

破解密码函数 如下:

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('没有加密呀~~~')

开始破解

代码已经准备好,下面,我们正式开始破解~~~

效果如下 ↓ ↓ ↓

几秒之后,密码破解成功。

emmm ,密码居然是 1314

完整代码

from os import error
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('加密成功!,')

def decrypt(old_Path):
    """
    :param old_Path: 待加密文件的路径名
    :param new_Path: 加密之后的文件路径名
    """
    with open(old_Path, 'rb') as pdfFile:
        pdfReader = PyPDF2.PdfFileReader(pdfFile)
        # 判断文件是否加密
        if pdfReader.isEncrypted:
            # 判断密码是否正确
            for i in range(10000):
                #生成四位数密码
                pwd=str(i).zfill(4).replace(' ','')
                print(pwd)
                try:
                    pdfReader.decrypt(pwd)
                except:
                    print('密码不对,哼~~~')
                else:
                    print('成功了!密码是:'+pwd)
                    break
        else:
            print("没有密码哦~")
if __name__ == '__main__':
    #给pdf加密
    #encrypt('E:/520快乐.pdf','E:/520快乐2.pdf')
    #给pdf解密,我们尝试 4位数的密码
    decrypt('E:/520快乐.pdf')

故事结尾

密码居然是1314

让我有点不知所措呢

迫不及待的打开 “520快乐.pdf”

啪啪啪

欢快的输入破解出的密码 1314

----The End----