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
相关推荐
开开心心_Every19 小时前
强制打字练习工具:打够百字才可退出
java·游戏·微信·eclipse·pdf·excel·语音识别
开开心心_Every19 小时前
多端免费远程控制工具:4K流畅同账号直连
游戏·macos·微信·pdf·excel·语音识别·phpstorm
心语星光21 小时前
用LibreOffice实现批量将pptx文件转换为pdf文件
开发语言·pdf·ppt
夏日白云1 天前
《PDF解析工程实录》第 17 章|内容流里“看得见却看不见”的字符:那些幽灵文字从哪来?
pdf·llm·大语言模型·rag·文档解析
2501_930707781 天前
使用C#代码检查 PDF 是否受密码保护并确认正确的密码
pdf
开开心心就好2 天前
图片格式转换工具,右键菜单一键转换简化
linux·运维·服务器·python·django·pdf·1024程序员节
貂蝉空大2 天前
vue-pdf-embed分页预览解决文字丢失问题
前端·vue.js·pdf
袁袁袁袁满2 天前
Python爬虫下载PDF文件
爬虫·python·pdf·python爬虫下载pdf文件
Knight_AL2 天前
docx4j vs LibreOffice:Java 中 Word 转 PDF 的性能实测
java·pdf·word
夏日白云2 天前
《PDF解析工程实录》第 14 章|内容流文本布局计算:pdfminer 在做什么,以及它为什么不够
pdf·llm·大语言模型·rag·文档解析