-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAKE.py
81 lines (73 loc) · 1.89 KB
/
LAKE.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
abc = "abcdefghijklmonpqrstuvwxyz"
def normalize(rot : int, base : int):
while rot > base:
rot -= base
return rot
def lshift(text : str, times : int):
for i in range(times):
text = text[1:] + text[0]
return text
def encrypt(text : str, key : str):
if not text.isalpha() or not key.isalpha():
raise TypeError("key or text should only be letters")
text = text.lower()
key = key.lower()
rotmax = len(key)
rot = -1
abc2 = ""
out = ""
keys = []
counter = -1
counter2 = -1
# get alphabet keys
for letter in key:
for letter2 in abc:
rot += 1
if letter == letter2:
abc2 = lshift(abc, rot)
break
keys += [abc2]
rot = -1
#print(keys)
# get cyphertext
for letter in text:
counter += 1
for letter2 in abc:
counter2 += 1
if letter == letter2:
out += keys[normalize(counter, rotmax) - 1][counter2]
counter2 = -1
break
return out
def decrypt(text : str, key : str):
if not text.isalpha() or not key.isalpha():
raise TypeError("key or text should only be letters")
text = text.lower()
key = key.lower()
rotmax = len(key)
rot = -1
abc2 = ""
out = ""
keys = []
counter = -1
counter2 = -1
#get alphabet keys
for letter in key:
for letter2 in abc:
rot += 1
if letter == letter2:
abc2 = lshift(abc, rot)
break
keys += [abc2]
rot = -1
#print(keys)
#get plaintext
for letter in text:
counter += 1
for letter2 in keys[normalize(counter, rotmax) - 1]:
counter2 += 1
if letter == letter2:
out += abc[counter2]
counter2 = -1
break
return out