前女友发来加密的 “520快乐.pdf“,我用python破解开之后,却发现。。。
日期: 2021-05-20 分类: 个人收藏 583次阅读
大家好,我是Lex 喜欢欺负超人那个Lex
520收到前女友发来的加密PDF文件,说打开之后有惊喜,难道是要复合?
我用python破解开之后,却发现。。。
划重点:1、如何使用python给pdf设置密码。2、如何使用python破解加密的pdf
python干货+剧情 满满。收藏收藏!!!
事情是这样的
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

完整代码
https://download.csdn.net/download/weixin_42350212/19777145
故事结尾
密码居然是1314
让我有点不知所措呢

迫不及待的打开 “520快乐.pdf”
啪啪啪
欢快的输入破解出的密码 1314

----The End----
推荐阅读
python实战
精华推荐

