forked from kal179/Beginners_Python_Examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaesars_cipher.py
More file actions
40 lines (33 loc) · 1.1 KB
/
caesars_cipher.py
File metadata and controls
40 lines (33 loc) · 1.1 KB
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
import string
def concat_elements(n):
res = ""
for i in n:
res += i
return res
def encrypt(message, key):
string_chars = list(string.ascii_uppercase) + list(string.ascii_lowercase) + list(string.digits) + list(string.punctuation) + [" "]
try:
splitted_message = list(message)
except TypeError:
return "Expected an string for message!"
for char in splitted_message:
try:
tmp = string_chars[string_chars.index(char) + key]
except IndexError:
tmp_key = (string_chars.index(char) + key) - len(string_chars)
tmp = string_chars[tmp_key]
splitted_message[splitted_message.index(char)] = tmp
final = concat_elements(splitted_message)
return final
def n_encrypt(ciph, key, times):
# You can modify this to take multiple keys
i = 0
ciph = ""
while i < times:
ciph = encrypt(ciph, key)
i += 1
return ciph
# Test
mesage = "This is top secret!"
result = encrypt(mesage, 9)
print("Encrypted Text: " + result)