Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 68 lines (52 sloc) 2.357 kB
c4fd6ab @asweigart Pylint-suggested fixes.
authored
1 # Transposition File Breaker
f16b361 @asweigart Initial commit.
authored
2 # http://inventwithpython.com/codebreaker (BSD Licensed)
3
c4fd6ab @asweigart Pylint-suggested fixes.
authored
4 import sys, time, os, sys, transpositionDecrypt, detectEnglish
f16b361 @asweigart Initial commit.
authored
5
6 inputFilename = 'frankenstein.encrypted.txt'
7 outputFilename = 'frankenstein.decrypted.txt'
8
9 def main():
10 if not os.path.exists(inputFilename):
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
11 print('The file %s does not exist. Quitting.' % (inputFilename))
f16b361 @asweigart Initial commit.
authored
12 sys.exit()
13
14 inputFile = open(inputFilename)
15 content = inputFile.read()
16 inputFile.close()
17
18 brokenMessage = breakTransposition(content)
19
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
20 if brokenMessage != None:
e280220 @asweigart More unit tests.
authored
21 print('Writing decrypted text to %s.' % (outputFilename))
f16b361 @asweigart Initial commit.
authored
22
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
23 outputFile = open(outputFilename, 'w')
24 outputFile.write(brokenMessage)
25 outputFile.close()
26 else:
27 print('Failed to break encryption.')
f16b361 @asweigart Initial commit.
authored
28
29
30 # The breakTransposition() function's code was copy/pasted from
31 # transpositionBreaker.py and had some modifications made.
32 def breakTransposition(message):
33 print('Breaking...')
34 # Python programs can be stopped at any time by pressing Ctrl-C (on
35 # Windows) or Ctrl-D (on Mac and Linux)
36 print('(Press Ctrl-C or Ctrl-D to quit at any time.)')
37
38 for key in range(1, len(message)):
39 print('Trying key #%s... ' % (key), end='')
40 sys.stdout.flush()
41
42 # We want to track the amount of time it takes to test a single key,
43 # so we record the time in startTime.
44 startTime = time.time()
45
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
46 decryptedText = transpositionDecrypt.decryptMessage(key, message)
47 englishPercentage = round(detectEnglish.getEnglishCount(decryptedText) * 100, 2)
f16b361 @asweigart Initial commit.
authored
48
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
49 totalTime = round(time.time() - startTime, 3)
50 print('Key test time: %s seconds, ' % (totalTime), end='')
51 sys.stdout.flush() # flush printed text to the screen
f16b361 @asweigart Initial commit.
authored
52
c4fd6ab @asweigart Pylint-suggested fixes.
authored
53 print('Percent English: %s%%' % (englishPercentage))
54 if detectEnglish.isEnglish(decryptedText, 20):
f16b361 @asweigart Initial commit.
authored
55 print()
56 print('Key ' + str(key) + ': ' + decryptedText[:100])
57 print()
58 print('Enter D for done, or just press Enter to continue:')
59 response = input('> ')
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
60 if response.strip().upper().startswith('D'):
f16b361 @asweigart Initial commit.
authored
61 return decryptedText
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
62 return None
f16b361 @asweigart Initial commit.
authored
63
64
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
65 # If transpositionFileBreaker.py is run (instead of imported as a module)
66 # call the main() function.
f16b361 @asweigart Initial commit.
authored
67 if __name__ == '__main__':
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
68 main()
Something went wrong with that request. Please try again.