UnicodeEncodeError:'latin-1'编解码器无法编码字符PyPDF2

时间:2016-11-23 06:38:31

标签: python unicode pypdf2

一个简单的初学者挑战; PDF文档的密码破解程序。

不知道为什么会发生这种情况。我会继续搜索网络以找到解决方案,但我相信这可能是我可以花一天时间而仍然无法解决的问题。不过我会在我的列表中添加编码,unicode等。

>>> 
 RESTART: C:\Users\Dave\Desktop\2016Coding\AutomateBoring\13-PDF\bruteforce.py 
2016-11-23 06:14:51,974 --  DEBUG -- variations: ['AARHUS', 'aarhus', 'Aarhus']
2016-11-23 06:14:52,010 --  DEBUG -- variation: AARHUS
Traceback (most recent call last):
  File "C:\Users\Dave\Desktop\2016Coding\AutomateBoring\13-PDF\bruteforce.py", line 64, in <module>
    main()
  File "C:\Users\Dave\Desktop\2016Coding\AutomateBoring\13-PDF\bruteforce.py", line 48, in main
    if pdf_reader.decrypt(variation) == 0:
  File "C:\Users\Dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 1987, in decrypt
    return self._decrypt(password)
  File "C:\Users\Dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 2017, in _decrypt
    val = utils.RC4_encrypt(new_key, val)
  File "C:\Users\Dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\utils.py", line 181, in RC4_encrypt
    retval += b_(chr(ord_(plaintext[x]) ^ t))
  File "C:\Users\Dave\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\utils.py", line 238, in b_
    r = s.encode('latin-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u0195' in position 0: ordinal not in range(256)
>>> 

脚本:

#! python3

# Brute force password breaker

# 10th word = ABANDONS

import docx
import logging
import PyPDF2

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s --  " +\
                    "%(levelname)s -- %(message)s")


def main():
    """Attempts to password break a pdf file using three variations of each
    word in a .txt file containing 44,000 total words"""


    with open("dictionary.txt", "r") as f:
        words = f.read().splitlines()

    with open("bruteforce2.pdf", "rb") as f:
        pdf_reader = PyPDF2.PdfFileReader(f)
        for word in words:
            variations = [word.upper(), word.lower(), word.title()]
            for variation in variations:
                logging.debug("variations: {}".format(variations))
                logging.debug("variation: {}".format(variation))
                if pdf_reader.decrypt(variation) == 0:
                    pass
                else:
                    print("\n*****The password is: {!r}  *****\n".format(word))
                    break



if __name__ == "__main__":
    main()

0 个答案:

没有答案