-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathcaesar_cipher.py
More file actions
90 lines (69 loc) · 2.09 KB
/
caesar_cipher.py
File metadata and controls
90 lines (69 loc) · 2.09 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
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
82
83
84
85
86
87
88
89
90
"""
Caesar Cipher Algorithm
The Caesar cipher is one of the simplest and most widely known encryption techniques.
It works by shifting each letter in the plaintext by a fixed number of positions
down or up the alphabet.
Example:
>>> encrypt("abc", 2)
'cde'
>>> decrypt("cde", 2)
'abc'
You can also encrypt/decrypt with uppercase letters:
>>> encrypt("Hello, World!", 3)
'Khoor, Zruog!'
>>> decrypt("Khoor, Zruog!", 3)
'Hello, World!'
Reference:
https://en.wikipedia.org/wiki/Caesar_cipher
"""
from string import ascii_lowercase, ascii_uppercase
def encrypt(text: str, shift: int) -> str:
"""
Encrypt the given text using Caesar cipher.
Args:
text: The input text to encrypt.
shift: The number of positions to shift each letter.
Returns:
The encrypted text as a string.
>>> encrypt("abc", 1)
'bcd'
>>> encrypt("xyz", 3)
'abc'
>>> encrypt("Hello, World!", 5)
'Mjqqt, Btwqi!'
"""
if not isinstance(text, str):
raise TypeError("Text must be a string.")
if not isinstance(shift, int):
raise TypeError("Shift must be an integer.")
result = []
for char in text:
if char in ascii_lowercase:
index = (ascii_lowercase.index(char) + shift) % 26
result.append(ascii_lowercase[index])
elif char in ascii_uppercase:
index = (ascii_uppercase.index(char) + shift) % 26
result.append(ascii_uppercase[index])
else:
result.append(char)
return "".join(result)
def decrypt(text: str, shift: int) -> str:
"""
Decrypt the given text encrypted with Caesar cipher.
Args:
text: The encrypted text to decrypt.
shift: The number of positions originally used to encrypt.
Returns:
The decrypted text as a string.
>>> decrypt("bcd", 1)
'abc'
>>> decrypt("abc", 3)
'xyz'
>>> decrypt("Mjqqt, Btwqi!", 5)
'Hello, World!'
"""
return encrypt(text, -shift)
if __name__ == "__main__":
import doctest
doctest.testmod()
print("✅ All doctests passed!")