2016年7月8日金曜日

開発環境

Automate the Boring Stuff with Python (Al Sweigart (著)、No Starch Press)のPart 2.(Automating Tasks)、Chapter 13.(Working with PDF and word Documents)、Practice Projects(Brute-Force PDF Password Breakder)(No. 7850)を取り組んでみる。

Practice Projects(Brute-Force PDF Password Breakder)(No. 7850)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys
import PyPDF2

if len(sys.argv) >= 1:
    filename = sys.argv[1]
else:
    print('usage: cmd password')
    sys.exit(1)

with open('dictionary.txt') as f:
    words = {line.strip() for line in f}

with open(filename, 'rb') as f:
    pdf_reader = PyPDF2.PdfFileReader(f)
    for word in words:
        if pdf_reader.decrypt(word) == 1:
            print(word.upper())
            break
        if pdf_reader.decrypt(word.lower()) == 1:
            print(word.lower())
            break
    else:
        print("couldn't find a password.")

入出力結果(Terminal, IPython)

$$ time ./pdf_password_breakder.py encrypted.pdf 
rosebud

real 82m6.105s
user 76m34.436s
sys 1m4.232s
$

0 コメント:

コメントを投稿

Comments on Google+: