Python编程:实现恺撒密码加解密(完整源码)
恺撒密码是一种古老的加密方式,它是将明文中的每个字母替换成字母表中的另一个字母。在这种替换方式中,每个字母会向后移动固定的偏移量,例如往后偏移3个字母即为“D”替换成“G”,“E”替换成“H”,以此类推。
在 Python 中,我们可以使用简单的代码来实现恺撒密码的加解密。下面是完整源代码:
def caesar_cipher(message, key, mode):
result = ''
if mode == 'encrypt':
for char in message:
if char.isalpha():
char_code = ord(char) + key
if char.isupper():
if char_code > ord('Z'):
char_code -= 26
elif char_code < ord('A'):
char_code += 26
elif char.islower():
if c