-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspositionBruteHacker.py
More file actions
48 lines (40 loc) · 1.98 KB
/
transpositionBruteHacker.py
File metadata and controls
48 lines (40 loc) · 1.98 KB
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
# ======================================
# Brute Hacking The Transposition Cipher
# ======================================
import decryptTranspositionCipher
import detectEnglish
# returns plaintext if a readable decryption is found, None otherwise
def hackTransposition(ciphertext):
print("Decrypting..")
print("This may take a while, press Ctrl-D to stop at any time.")
# Brute-force: Loop through every possible key
# keys for transposition cipher range from 1 to length of message
for key in range(1, len(ciphertext)):
print("Trying Key #%s...", (key))
plaintext = decryptTranspositionCipher.decryptMessage(key, ciphertext)
# if plaintext is readable, ask user if we should stop hacking
if detectEnglish.isEnglish(plaintext):
print()
print('Possible plaintext:')
print('Key: %s, Message: %s', (key, plaintext[:100]))
print()
print('Enter Y if done, anything else to continue hacking:')
response = input('> ')
# return PT if user satisfied with match
if response.strip().upper() == 'Y':
return plaintext
# if we loop through all keys and none matched a potential plaintext, return None
return None
def main():
ciphertext = """AaKoosoeDe5 b5sn ma reno ora'lhlrrceey e enlh na indeit n uhoretrm au ieu v er Ne2 gmanw,forwnlbsya apor tE.no euarisfatt e mealefedhsppmgAnlnoe(c -or)alat r lw o eb nglom,Ain one dtes ilhetcdba. t tg eturmudg,tfl1e1 v nitiaicynhrCsaemie-sp ncgHt nie cetrgmnoa yc r,ieaa toesa- e a0m82e1w shcnth ekh gaecnpeutaaieetgn iodhso d ro hAe snrsfcegrt NCsLc b17m8aEheideikfr aBercaeu thllnrshicwsg etriebruaisss d iorr."""
plaintext = hackTransposition(ciphertext)
if plaintext == None:
print()
print('Unable to decrypt.')
else:
print()
print('Original Plaintext:')
print('===================')
print(plaintext)
if __name__ == '__main__':
main()