Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 50 lines (36 sloc) 2.331 kB
f16b361 @asweigart Initial commit.
authored
1 # Transpositional Cipher Breaker
2 # http://inventwithpython.com/codebreaker (BSD Licensed)
3
4 import pyperclip, detectEnglish, transpositionDecrypt
5
6 def main():
7 # You might want to copy & paste this text from the source code at
24fc561 @asweigart Changes from unit testing.
authored
8 # http://invpy.com/transpositionBreaker.py
f16b361 @asweigart Initial commit.
authored
9 myMessage = """Cb b rssti aieih rooaopbrtnsceee er es no npfgcwu plri ch nitaalr eiuengiteehb(e1 hilincegeoamn fubehgtarndcstudmd nM eu eacBoltaeteeoinebcdkyremdteghn.aa2r81a condari fmps" tad l t oisn sit u1rnd stara nvhn fsedbh ee,n e necrg6 8nmisv l nc muiftegiitm tutmg cm shSs9fcie ebintcaets h aihda cctrhe ele 1O7 aaoem waoaatdahretnhechaopnooeapece9etfncdbgsoeb uuteitgna.rteoh add e,D7c1Etnpneehtn beete" evecoal lsfmcrl iu1cifgo ai. sl1rchdnheev sh meBd ies e9t)nh,htcnoecplrrh ,ide hmtlme. pheaLem,toeinfgn t e9yce da' eN eMp a ffn Fc1o ge eohg dere.eec s nfap yox hla yon. lnrnsreaBoa t,e eitsw il ulpbdofgBRe bwlmprraio po droB wtinue r Pieno nc ayieeto'lulcih sfnc ownaSserbereiaSm-eaiah, nnrttgcC maciiritvledastinideI nn rms iehn tsigaBmuoetcetias rn"""
10
11 brokenCiphertext = breakTransposition(myMessage)
12
13 if brokenCiphertext == None:
dd74989 @asweigart More changes from unit testing
authored
14 print('Failed to break encryption.')
f16b361 @asweigart Initial commit.
authored
15 else:
16 print('Copying broken ciphertext to clipboard:')
24fc561 @asweigart Changes from unit testing.
authored
17 print(brokenCiphertext)
f16b361 @asweigart Initial commit.
authored
18 pyperclip.copy(brokenCiphertext)
19
20
21 def breakTransposition(message):
22 print('Breaking...')
23
24 # Python programs can be stopped at any time by pressing Ctrl-C (on
25 # Windows) or Ctrl-D (on Mac and Linux)
26 print('(Press Ctrl-C or Ctrl-D to quit at any time.)')
27
28 # brute force by looping through every possible key
29 for key in range(1, len(message)):
e280220 @asweigart More unit tests.
authored
30 print('Trying key #%s...' % (key))
f16b361 @asweigart Initial commit.
authored
31
32 decryptedText = transpositionDecrypt.decryptMessage(key, message)
33
34 if detectEnglish.isEnglish(decryptedText):
24fc561 @asweigart Changes from unit testing.
authored
35 # Check with user to see if the decrypted key has been found.
f16b361 @asweigart Initial commit.
authored
36 print()
37 print('Possible encryption break:')
38 print('Key ' + str(key) + ': ' + decryptedText[:100])
39 print()
40 print('Enter D for done, or just press Enter to continue breaking:')
41 response = input('> ')
42
c4fd6ab @asweigart Pylint-suggested fixes.
authored
43 if response.strip().upper().startswith('D'):
f16b361 @asweigart Initial commit.
authored
44 return decryptedText
45
46 return None
47
48 if __name__ == '__main__':
49 main()
Something went wrong with that request. Please try again.