如何使用pypdf2检查pdf密码是否正确

时间:2019-05-02 13:38:33

标签: python python-3.x pypdf2

我正在使用“用Python自动化乏味的东西”做一个挑战性的问题。任务是编写一个程序,该程序将使用提供的dictionary.txt文件“强力”使用pdf密码。

使用我加密并知道密码的文件以及包含该密码的字典文件,我无法获取代码来“弄清楚”它是密码。而是运行到字典文件的末尾,然后停止。

我以为我可能会误解pdfObject.decrypt()在“ if”语句中的工作方式,因此我编写了一个小测试程序来使用该语法,但是该程序在该程序中使用的语法与我的“主”程序。在“测试”程序中,我从input()而不是argv和列表中提供密码,但是我看不到是否/如何影响密码。

#My Program (that blitzes right past the correct password):

#/usr/bin/python

# bruteForce.py - Uses a dictionary attack to crack the password of an
# encrypted pdf

from sys import argv
import PyPDF2

# Take the argument for the file you want to attack and open it
if len(argv) > 1:
    pdfFilename = argv[1]
else:
    print('User must specify a file.')
    exit()

# Open the pdf as an object
pdfFile = open(pdfFilename, 'rb')
pdfObject = PyPDF2.PdfFileReader(pdfFile)

# Add the contents of the dictionary file to a data structure
dictionaryList = []
dictionaryFile = open('dictionary.txt')
for word in dictionaryFile.readlines():
    dictionaryList.append(word)
    wordLower = word.lower()
    dictionaryList.append(wordLower)
dictionaryFile.close()

# Iterate over the data structure, trying each password as lowercase
print('Trying passwords...')
for word in dictionaryList:
    password = str(word)
    print('Trying ' + password)
    if pdfObject.decrypt(password) == 1:
        print('Password is ' + word)
        exit()
    else:
        continue

print('Password not found')

##My Test (that works and returns 'Yup, that's it!'):

#!/usr/bin/python
# Figuring out how to deal with foo.decrypt to get a value of 1 or 0
# This first test returns the correct result with the correct password,
# so presumably bruteForce is not feeding it the right password somehow

import PyPDF2

filename = input('What file?')
password = input('What password?')

pdfFile = open(filename, 'rb')
pdfObject = PyPDF2.PdfFileReader(pdfFile)

if pdfObject.decrypt(password) == 1:
    print('Yup, that\'s it!')
else:
    print('Nope!')



I expect the program to arrive at the correct word in the dictionary, try it, and stop. Instead, it runs to the end of the list and says "Password not found."

1 个答案:

答案 0 :(得分:0)

词典条目包含文本文件中的换行符,因此它们与密码不匹配。在将它们添加到字典中之前,我用wordStripped = word.strip('\n')对其进行了剥离,该程序可以按预期运行(速度约为后者的两倍)。