-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptanal.py
executable file
·121 lines (108 loc) · 4.01 KB
/
cryptanal.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
#!/usr/bin/env python
"""
This program is a basic cryptanalysis tool
useful for finally finding out what little Susie is writing to Billy
or what Caeser is writing to the Romans
This is meant to be a parent class. Write subclasses to this interface
for added functionality
"""
import sys
class CryptAnal():
def __init__(self, wordlist):
self.letterlist = wordlist
self.totalnum = len(wordlist)
def __calcfreq__(self, letters):
#calculate the frequencies
dictionary = {}
for i in letters:
if i in dictionary:
dictionary[i] += 1
else:
dictionary[i] = 1
#freqlist stores an unsorted list of the frequencies
self.freqlist = [(a,b) for b,a in dictionary.items()]
def substitute(self, fromstr, tostr, startrange=0, endrange=None, jumpval=1):
#setting the endrange default value to the strlen
if endrange == None:
endrange = int(self.totalnum) - 1
for i in range (startrange, endrange, jumpval):
if self.letterlist[i] == fromstr:
self.letterlist = self.letterlist[:i] + tostr + self.letterlist[i+1:]
#this shifts letters.
#eg 'abcdefg' shifted by defaults will give 'bcdefga'
def transpose(self, shiftl=1, startrange=0, endrange=None):
#set default endrange
if endrange == None:
endrange = int(len(self.letterlist)-1)
#make a list so we can swap the elements around
templist = []
for i in self.letterlist:
templist.append(i)
tempvar = self.letterlist[startrange + shiftl - 1]
for i in range(startrange, endrange):
templist[i] = self.letterlist[(i+shiftl)%(endrange+1)]
templist[endrange] = tempvar
self.letterlist = "".join(templist)
#this function checks the current letters to see if they are printable or not
def checksifPrintable(self):
canprintAscii = True
print "bleh"
for i in self.letterlist:
if ord(i) <= 6 or (ord(i) >= 16 and ord(i) <= 31):
canprintAscii = False
return canprintAscii
#this prints the letters in the list
def printcrypt(self):
#see if letters are printable or not
canprintAscii = self.checksifPrintable()
#canprintAscii = True
print "Hiya"
if canprintAscii:
#all characters can be printed to the screen
print self.letterlist
else:
#there are some weird chars in this text
print "There are some weird chars in this text so I can't print it out as ascii"
print "Printing in hex..."
for i in self.letterlist:
print "%#x" % ord(i),
"""
#this prints the letters in the list
def printcrypt(self):
print self.letterlist
"""
#print a sorted count of all letters
#this needs work - should be able to sort on multiple items, have different indices, etc
#def printsorteddict(self, letterlist=None, startrange=0, endrange=None, jumpval=1):
def printsorteddict(self, letterlist=None):
#get default letter list
if letterlist == None:
letterlist = self.letterlist
self.__calcfreq__(letterlist)
self.freqlist.sort()
self.freqlist.reverse()
print "Frequency\tLetter\t\tAscii\t\tRatio"
print "--------------------------------"
for i in self.freqlist:
if i[1] != '\n':
print i[0], "\t\t", str(i[1]),"\t\t",ord(i[1]),"\t\t", float(i[0])/self.totalnum
else:
print i[0], "\t\t", '\\n',"\t",ord(i[1]),"\t", i[0]/self.totalnum
#this sets the letters in the list (this is handy if you
#do encryption not using the substitute, or transpose
#functions to do an encryption/manipulation.
def setcrypt(self, cryptlist):
self.letterlist = cryptlist
self.totalnum = len(cryptlist)
#this returns the letters in the list (this is handy if you
#want to get the letters in the current object, but don't
#want to print them. Again a function most useful if you
#don't intend to use the built in encryption tools.
def getcrypt(self):
return self.letterlist
def getfreq(self):
self.__calcfreq__(self.letterlist)
dict = {}
for i in self.freqlist:
dict[i[1]] = float(i[0])/self.totalnum
return dict