-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstitution.py
64 lines (49 loc) · 1.57 KB
/
substitution.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
#!/usr/bin/env python
import sys
import freqanalysis
class Substitution:
def __init__(self, args):
args = args.replace(' ', '')
self.cipherText = args
self.cipherLength = len(args)
def mainLoop(self):
while 1:
command=raw_input('What would you like to do? (Press "show" to show your options) >').lstrip()
if command.lower().startswith('show'):
self.showOptions()
elif command.lower().startswith('print'):
self.printFrequency()
elif command.strip().lower() == "exit":
print "leaving substitution..."
break
else:
print "Error: command not recognized. Press s to show your options."
def showOptions(self):
print 'print'
print 'exit'
print 'show'
def getFreq(self, val):
freq = float(self.cipherText.count(val))
return (freq/self.cipherLength)
def getFreqTable(self):
letterList = []
for i in self.cipherText:
if letterList.count(i) == 0:
letterList.append(i)
cipherFreqTable = {}
for c in letterList:
freq = self.getFreq(c)
cipherFreqTable[c] = freq
return cipherFreqTable
def getFrequency(self):
cipherFreqTable = self.getFreqTable()
myFreq = freqanalysis.xfrequencyTable(cipherFreqTable)
def printFrequency(self):
cipherFreqTable = self.getFreqTable()
myFreq = freqanalysis.xfrequencyTable(cipherFreqTable)
myFreq.printEnglishvFreq()
if __name__ == "__main__":
stringToCrypt = sys.argv[1]
print 'Cipher Text is: ' + str(stringToCrypt)
myAnalysis = Substitution(stringToCrypt)
myAnalysis.getFrequency()