温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用python破解加密的pdf

发布时间:2021-08-20 09:15:38 来源:亿速云 阅读:297 作者:小新 栏目:开发技术

这篇文章主要介绍了如何用python破解加密的pdf,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1、代码中可以自定义代码生成函数,生成各种组合的密码,进行破解

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

如何用python破解加密的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

如何用python破解加密的pdf

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

如何用python破解加密的pdf

感谢你能够认真阅读完这篇文章,希望小编分享的“如何用python破解加密的pdf”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI