-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeaufortautokeyC.py
49 lines (43 loc) · 1.11 KB
/
beaufortautokeyC.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
def encrypt(txt, key):
if key == "":
key = "abc"
converted_text = ""
if len(key) > len(txt):
key = key[: len(txt)]
elif len(key) < len(txt):
key += txt
key = key[:len(txt)]
# key += txt[:len(key)]
for i in range(len(txt)):
if txt[i].isupper():
v = 'A'
elif txt[i].islower():
v = 'a'
else:
converted_text += txt[i]
continue
s = ord(key[i]) - ord(txt[i])
if s < 0:
s += 26
converted_text += chr(s + ord(v))
return converted_text
def decrypt(txt, key):
if key == "":
key = "abc"
converted_text = ""
for i in range(len(txt)):
b_k = ""
if txt[i].isupper():
v = 'A'
elif txt[i].islower():
v = 'a'
else:
converted_text += txt[i]
continue
s = ord(key[i]) - ord(txt[i])
if s < 0:
s += 26
b_k = chr(s + ord(v))
key += b_k
converted_text += b_k
return converted_text