-
Notifications
You must be signed in to change notification settings - Fork 2
/
Simple Cipher.py
30 lines (26 loc) · 1009 Bytes
/
Simple Cipher.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
from random import randint
class Cipher:
def __init__(self, key=None):
if key == None:
self.key = "".join([chr(randint(0, 25) + ord("a")) for _ in range(26)])
else:
self.key = key
def encode(self, text):
if self.key == None:
self.key = "a" * len(text)
length_key = len(self.key)
for i in range(len(text)):
aux = ord(text[i]) + ord(self.key[i % (length_key)]) - 2*ord("a")
text = text[:i] + chr(aux % 26 + ord("a")) + text[i + 1:]
return text
def decode(self, text):
if self.key == None:
self.key = "a" * len(text)
length_key = len(self.key)
for i in range(len(text)):
aux = ord(text[i]) - ord(self.key[i % (length_key)])
if aux >= 0:
text = text[:i] + chr(aux + ord("a")) + text[i + 1:]
continue
text = text[:i] + chr(aux + ord("z") + 1) + text[i + 1:]
return text