-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcaesar.py
More file actions
77 lines (58 loc) · 1.38 KB
/
Copy pathcaesar.py
File metadata and controls
77 lines (58 loc) · 1.38 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
"""
*
* 凯撒密码 - 简单的替换加密
*
* 问题:将字母表中的每个字母移动固定位数
*
* 核心思想:
* - 字母替换
* - 循环移位
* - 保持大小写
*
* 时间复杂度: O(n)
* 空间复杂度: O(n)
"""
def caesar_encrypt(text: str, shift: int) -> str:
"""
凯撒加密
Args:
text: 明文
shift: 移位数
Returns:
密文
"""
result = []
for char in text:
if char.isupper():
result.append(chr((ord(char) - ord('A') + shift) % 26 + ord('A')))
elif char.islower():
result.append(chr((ord(char) - ord('a') + shift) % 26 + ord('a')))
else:
result.append(char)
return ''.join(result)
def caesar_decrypt(text: str, shift: int) -> str:
"""
凯撒解密
Args:
text: 密文
shift: 移位数
Returns:
明文
"""
return caesar_encrypt(text, 26 - (shift % 26))
def main():
"""主函数"""
print("=== 凯撒密码 ===")
text = "Hello, World!"
shift = 3
print(f"明文: {text}")
print(f"移位数: {shift}")
# 加密
encrypted = caesar_encrypt(text, shift)
print(f"加密后: {encrypted}")
# 解密
decrypted = caesar_decrypt(encrypted, shift)
print(f"解密后: {decrypted}")
print(f"验证: {text == decrypted}")
if __name__ == "__main__":
main()