以下是用Python实现凯撒加密的代码:
```python
def caesar_cipher(plaintext, shift):
ciphertext = ""
for char in plaintext:
if char.isalpha():
# Shift the character by the given amount
shifted_char = chr((ord(char) - 65 + shift) % 26 + 65)
else:
shifted_char = char
ciphertext += shifted_char
return ciphertext
# Example usage
plaintext = "HELLO WORLD"
shift = 3
ciphertext = caesar_cipher(plaintext, shift)
print(ciphertext) # prints "KHOOR ZRUOG"
```
在这个实现中,我们定义了一个名为`caesar_cipher`的函数,它接受两个参数:明文和移位数。然后,我们遍历明文中的每个字符,并将其移位。如果字符是字母,则我们使用ASCII码将其转换为数字,将其移位,然后将其转换回字符。最后,我们将所有移位后的字符组合成一个字符串并返回。
在