您的位置:首页 > 编程语言 > Python开发

前女友发来加密的 “520快乐.pdf“,我用python破解开之后,却发现。。。

2021-05-20 22:54 2071 查看

大家好,我是Lex 喜欢欺负超人那个Lex

520收到前女友发来的加密PDF文件,说打开之后有惊喜,难道是要复合?

我用python破解开之后,却发现。。。

划重点:1、如何使用python给pdf设置密码。2、如何使用python破解加密的pdf 

python干货+剧情 满满。收藏收藏!!!

事情是这样的

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

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

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

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

想复合就直说嘛

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

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

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

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

 

明确需求

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

2、520快乐.pdf  如下 < 20000 span style="color:#fe2c24;">↓ ↓ ↓  加密了打不开

 

安装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----

 

推荐阅读

python实战

【python实战】昨晚,我用python帮隔壁小姐姐P证件照 自拍,然后发现...

【python实战】女友半夜加班发自拍 python男友用30行代码发现惊天秘密

【python实战】python你TM太皮了——区区30行代码就能记录键盘的一举一动

python实战】女神相册密码忘记了,我只用Python写了20行代码~~~

渗透测试

【渗透案例】上班摸鱼误入陌生网址——结果被XSS劫持了

【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战

【渗透学习】Web安全渗透详细教程+学习线路+详细笔记【全网最全+建议收藏】

【渗透案例】如何用ssh工具连接前台小姐姐的“小米手机”——雷总看了直呼内行!!!

【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战

pygame系列文章

一起来学pygame吧 游戏开发30例(二)——塔防游戏

一起来学pygame吧 游戏开发30例(三)——射击外星人小游戏

一起来学pygame吧 游戏开发30例(四)——俄罗斯方块小游戏

一起来学pygame吧 游戏开发30例(五)——消消乐 小游戏

一起来学pygame吧 游戏开发30例(六)——高山滑雪 小游戏

 

CSDN官方学习推荐 ↓ ↓ ↓

CSDN出的Python全栈知识图谱,太强了,推荐给大家!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: