-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.py
169 lines (143 loc) · 5.47 KB
/
caesar.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from math import pow
#Decrypt a Caesar Cipher
def main():
vigDecryptLen(vigenereCipher,7)
print "hello"
englDict = {
'a': .08167,
'b': .01492,
'c': .02782,
'd': .04253,
'e': .12702,
'f': .02228,
'g': .02015,
'h': .06094,
'i': .06996,
'j': .00153,
'k': .00772,
'l': .04025,
'm': .02406,
'n': .06749,
'o': .07507,
'p': .01929,
'q': .00095,
'r': .05987,
's': .06327,
't': .09056,
'u': .02758,
'v': .00978,
'w': .02360,
'x': .00150,
'y': .01974,
'z': .00074,
}
vigenereCipher = """lusgdwvkxwaaezaaoplsasyzkzwnuemlweasymowxbvrrqfigzrzaqkgzbtpkixwlkkuhhkrreqxuwcchbeyhuoxhxvufixvxvlhherrpacvausaurhdhprfthdrjgzryababjqwogmfubjauiqlusyucclnbwocflywjhwhhgvafmczrfphbgaabyrpheagskiewawowlrdrbcwlbxgvanilobfzwlrfroykgbdhajfeatrhnhegwqoowlruvddhvgwkhkiezgacwotusosplgfmogplxhlvcjfmczrfwvwhuuswflpgyiiqgnfoswwxnuxszzmgzsfatyrfpmwqeyqfwowlrcngevovwkoilrnlvcjlrigyjavpbgxwjjjbjfhnlrtkbtykeesphauwgznhwuiewcswwiqaahdhgvhusnwiklgvavxeaauovlbmyrxhxujrsykeesphauwygaukuqbjrtkuxuwrlwpmasgwkqxbtrgqfgrkftqoxuwahdhhvkgojfiftrhshiaubbohghlvjargpmefaqgrkbtpkiflewjjwnjrzeniyqgcxhqhdgwloifgshdhprfthdrjgzryababjqhdxwsaareqkzgesnhtrsgszvxeaauoqeejbkogsjfgvassfkvphhprfthdvsslusghcjgerolrpwjsydrgsxspkitjrophwgubairrqaiworvbxnzhwlrvvgpdrpwfhdhvrsfcjwlvkgsowabjxgevxusgwbdvrhrophhflewjjspuhfolrgzrdhdmalrlpdrqluszlwgsaqaeigorsjfsejrglrrqaauykeesphauwvknaqoxvhyskixuwxsuzsevysjjxulusghcjgerhhxgwegslpydvbaxtvfgvavezwjouzmgzocpkspuhfnhrpwfcbwlrkgfeqk"""
plainTxt = """ethicslawanduniversitypoliciestodefendasystemyouneedtobeabletothinklikeanattackerandthatincludesunderstandingtechniquesthatcanbeusedtocompromisesecurityhoweverusingthosetechniquesintherealworldmayviolatethelawortheuniversitysrulesanditmaybeunethicalundersomecircumstancesevenprobingforweaknessesmayresultinseverepenaltiesuptoandincludingexpulsioncivilfinesandjailtimeourpolicyineecsisthatyoumustrespecttheprivacyandpropertyrightsofothersatalltimesorelseyouwillfailthecourseactinglawfullyandethicallyisyourresponsibilitycarefullyreadthecomputerfraudandabuseactcfaaafederalstatutethatbroadlycriminalizescomputerintrusionthisisoneofseverallawsthatgovernhackingunderstandwhatthelawprohibitsyoudontwanttoenduplikethisguyifindoubtwecanreferyoutoanattorneypleasereviewitsspoliciesonresponsibleuseoftechnologyresourcesandcaenspolicydocumentsforguidelinesconcerningproperuseofinformationtechnologyatumaswellastheengineeringhonorcodeasmembersoftheuniversityyouarerequiredtoabidebythesepolic"""
def getVarianceOf(freqDict):
total = 0
for i in freqDict:
total += pow(freqDict[i] - (1/(0.0 + len(freqDict))),2)
total /= (len(freqDict)+0.0)
return total
def getVarianceAgainstEngl(otherDict):
total = 0
for i in englDict:
if(otherDict.has_key(i)):
total += pow(otherDict[i] - englDict[i],2)
else:
total += pow(englDict[i],2)
total /= 26.0
return total
def getFreqDict(str):
freqDict = {}
for x in str:
if freqDict.has_key(x):
freqDict[x] = freqDict[x] + 1
else:
freqDict[x] = 1
for x in freqDict:
freqDict[x] = freqDict[x] / (0.0 + len(str))
return freqDict
def getFreqDictsByLen(str, length):
dicts = []
for i in range(length):
dicts.append({})
for i in range(len(str)):
k = i%length
if dicts[k].has_key(str[i]):
dicts[k][str[i]] += 1
else:
dicts[k][str[i]] = 1
for d in dicts:
for i in d:
d[i] = d[i] / (0.0 + len(str)/length)
return dicts
#get vig cipher and find the corresponding freq's
def getFreqDictsVig(str,key):
str = vigCipher(str, key)
dicts = []
for i in range(len(key)):
dicts.append({})
for i in range(len(str)):
k = i%len(key)
if dicts[k].has_key(str[i]):
dicts[k][str[i]] += 1
else:
dicts[k][str[i]] = 1
for d in dicts:
for i in d:
d[i] = d[i] / (0.0 + len(str)/len(key))
return dicts
def getMeanFreqDicts(dicts):
total = 0
for d in dicts:
total += getVarianceOf(d)
return total / (0.0 + len(dicts))
def getMeanVigCipher(str, key):
vFreqDict = getFreqDictsVig(str, key)
return getMeanFreqDicts(vFreqDict)
def getVigChar(char, key):
offset = ((ord(key) - ord('a')) + (ord(char) - ord('a')))%26
return chr(ord('a') + offset)
def getDecryptChar(char, key):
offset = ((ord(char) - ord('a'))-(ord(key) - ord('a')) )%26
return chr(ord('a') + offset)
def vigCipher(str, key):
newStr = ''
for i in range(len(str)):
newStr += getVigChar(str[i],key[i%len(key)])
return newStr
def vigDecryptLen(str, length):
strings = []
key = ''
for i in range(length):
strings.append('')
for i in range(len(str)):
strings[i%length] += str[i]
for i in strings:
key += caesarDecrypt(i)
return key
#returns character most likely to be key
def caesarDecrypt(str):
keyVariance = []
for key in range(26):
newStr = ''
for i in str:
newStr += getDecryptChar(i,chr(ord('a')+key))
aDict = getFreqDict(newStr)
engVar = getVarianceAgainstEngl(aDict)
keyVariance.append(engVar)
return chr(ord('a')+keyVariance.index(min(keyVariance)))
def shiftByLen(str,length):
newStr = ''
for i in range(len(str)):
newStr += str[(i+length)%len(str)]
return newStr
def getCoincidences(str,length):
top = str
bot = shiftByLen(str, length)
coinc = 0
for i in range(len(str)):
if top[i] == bot[i]:
coinc += 1
return coinc