pdf查看密码

pdf有两种密码方式,一种是打开后进入文件内容页面后需要密码才能进行修改等操作,网上有很多方式进行移除密码操作,第二种是打开就需要密码,我这里简单记录一个暴力破解的方式,仅供参考

python 复制代码
import PyPDF2
import itertools
import string

def decrypt_pdf(input_file, output_file, password):
    with open(input_file, "rb") as file:
        reader = PyPDF2.PdfReader(file)

        if reader.is_encrypted:
            try:
                if reader.decrypt(password):
                    writer = PyPDF2.PdfWriter()

                    for page in reader.pages:
                        writer.add_page(page)

                    with open(output_file, "wb") as output_pdf_file:
                        writer.write(output_pdf_file)
                        print(f"解密后文件已保存为: {output_file}")
                        return True
                else:
                    print(f"密码'{password}'错误")
            except Exception as e:
                print(f"解密失败: {e}")
        else:
            print("该 PDF 文件没有加密。")
        return False

def generate_passwords(length):
    characters = string.ascii_letters + string.digits  # 字母加数字方式 可以添加其他字符
    for password in itertools.product(characters, repeat=length):
        yield ''.join(password)

if __name__ == "__main__":
    input_pdf_path = r"d:\"  # 输入你的加密 PDF 文件路径
    output_pdf_path = r"d:\"  # 输出解密后文件的路径

    # 尝试生成长度为1到4的所有可能的密码
    for length in range(1, 5):  # 可调整最大长度
        print(f"尝试长度为 {length} 的密码...")
        for password in generate_passwords(length):
            if decrypt_pdf(input_pdf_path, output_pdf_path, password):
                break
相关推荐
开开心心就好1 天前
批量PDF转换工具,一键转换Word Excel
开发语言·前端·学习·pdf·电脑·word·excel
老家的回忆2 天前
jsPDF和html2canvas生成pdf,组件用的elementplus,亲测30多页,20s实现
前端·vue.js·pdf·html2canvas·jspdf
Vertira2 天前
pdf 合并 python实现(已解决)
前端·python·pdf
十一0829932 天前
【PDF-XSS攻击】springboot项目-上传文件-解决PDF文件XSS攻击
spring boot·pdf·xss
qq_393828222 天前
PDF的图片文字识别工具
windows·pdf·电脑·软件需求·图片处理
wsxqaz2 天前
浏览器原生控件上传PDF导致hash值不同
算法·pdf·哈希算法
工业3D_大熊11 天前
3D模式格式转换工具HOOPS Exchange如何将3D PDF转换为STEP格式?
3d·pdf·3d格式转换·3d模型格式转换·cad格式转换·cad数据格式转换·3d模型可视化
IDRSolutions_CN12 天前
在 Java 中生成 PDF 缩略图(教程)
java·经验分享·pdf·软件工程·团队开发
IDRSolutions_CN12 天前
用Java将PDF转换成GIF
java·经验分享·pdf·软件工程·团队开发
贤和兄12 天前
使用docx4j 实现word转pdf(linux乱码处理)
linux·pdf·word