-
Notifications
You must be signed in to change notification settings - Fork 148
/
transpositionFileHacker.py
68 lines (52 loc) · 2.27 KB
/
transpositionFileHacker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Transposition File Hacker
# http://inventwithpython.com/hacking (BSD Licensed)
import sys, time, os, sys, transpositionDecrypt, detectEnglish
inputFilename = 'frankenstein.encrypted.txt'
outputFilename = 'frankenstein.decrypted.txt'
def main():
if not os.path.exists(inputFilename):
print('The file %s does not exist. Quitting.' % (inputFilename))
sys.exit()
inputFile = open(inputFilename)
content = inputFile.read()
inputFile.close()
brokenMessage = hackTransposition(content)
if brokenMessage != None:
print('Writing decrypted text to %s.' % (outputFilename))
outputFile = open(outputFilename, 'w')
outputFile.write(brokenMessage)
outputFile.close()
else:
print('Failed to hack encryption.')
# The hackTransposition() function's code was copy/pasted from
# transpositionHacker.py and had some modifications made.
def hackTransposition(message):
print('Hacking...')
# Python programs can be stopped at any time by pressing Ctrl-C (on
# Windows) or Ctrl-D (on Mac and Linux)
print('(Press Ctrl-C or Ctrl-D to quit at any time.)')
for key in range(1, len(message)):
print('Trying key #%s... ' % (key), end='')
sys.stdout.flush()
# We want to track the amount of time it takes to test a single key,
# so we record the time in startTime.
startTime = time.time()
decryptedText = transpositionDecrypt.decryptMessage(key, message)
englishPercentage = round(detectEnglish.getEnglishCount(decryptedText) * 100, 2)
totalTime = round(time.time() - startTime, 3)
print('Test time: %s seconds, ' % (totalTime), end='')
sys.stdout.flush() # flush printed text to the screen
print('Percent English: %s%%' % (englishPercentage))
if englishPercentage > 20:
print()
print('Key ' + str(key) + ': ' + decryptedText[:100])
print()
print('Enter D for done, or just press Enter to continue:')
response = input('> ')
if response.strip().upper().startswith('D'):
return decryptedText
return None
# If transpositionFileHacker.py is run (instead of imported as a module)
# call the main() function.
if __name__ == '__main__':
main()