用python实现凯撒加密,输入明文和一个数字,输出密文

动态 未结 0 72
小小草
小小草 LV1 2023年9月23日 23:40 发表
以下是用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码将其转换为数字,将其移位,然后将其转换回字符。最后,我们将所有移位后的字符组合成一个字符串并返回。 在
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复