-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaesarBoxChiper.py
42 lines (30 loc) · 1020 Bytes
/
CaesarBoxChiper.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
class CaesarBox:
def encode(self, msg, key):
msg = self.split(msg, key)
return self.encode_decode(msg, key)
def decode(self, msg, key):
width = self.calculate_width(msg, key)
msg = self.split(msg, width)
return self.encode_decode(msg, width)
def encode_decode(self, msg, key):
output = []
for index, chunk in ((i, c) for i in range(0, key) for c in msg):
try:
output.append(chunk[index])
except:
pass
return ''.join(output)
def split(self, msg, width):
return [msg[i:i + width] for i in range(0, len(msg), width)]
def calculate_width(self, msg, key):
if float(len(msg) / key) > int(len(msg)/key):
return int(len(msg)/key) + 1
return int(len(msg)/key)
key = 3
msg = "Hello World"
print ("Plain: %s" %(msg))
c = CaesarBox()
txt = c.encode(msg, key)
print ("Encoded: %s" %(txt))
txt = c.decode(txt, key)
print ("Decoded: %s" %(txt))