-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotational_cipher.py
35 lines (32 loc) · 1.03 KB
/
rotational_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
31
32
33
34
35
"""
Function for encoding a rotational cipher (rot13),
also known as a Caesar cipher.
Args:
text: string of text to encoded
key: integer value for shifting Ciphertext
Returns:
encoded: the encoded text of the ciphers
Examples:
- ROT5 `omg` gives `trl`
- ROT0 `c` gives `c`
- ROT26 `Cool` gives `Cool`
- ROT13 `The quick brown fox` gives `Gur dhvpx oebja sbk`
"""
from string import ascii_lowercase
def rotate(text, key):
"Rotational or Caesar cipher encoding"
letters = ascii_lowercase
encoded = ''
for character in text:
if character.isalpha():
index = letters.index(character.lower()) + key
if index > 25:
index = index - 26
encoded_letter = letters[index]
if character.isupper():
encoded = encoded + encoded_letter.upper()
else:
encoded = encoded + encoded_letter
else:
encoded = encoded + character
return encoded