用python实现对明文为leave at zero in the night的凯撒加密
明文为:“leave at zero in the night”
凯撒加密是一种简单的替换加密方法,将明文中的每个字母都按照字母表中后面的几个字母替换,例如,向后移动3个字母,即将A替换成D,B替换成E,以此类推。
下面是用Python实现对明文进行凯撒加密的代码:
plaintext = "leave at zero in the night"
key = 3 # 向后移动3个字母
ciphertext = ""
for char in plaintext:
if char.isalpha():
# 如果是字母,就按照凯撒加密的规则进行替换
new_char = chr((ord(char) - ord('a') + key) % 26 + ord('a'))
else:
# 如果不是字母,就直接保留原字符
new_char = char
ciphertext += new_char
print("明文:", plaintext)
print("密文:", ciphertext)
输出结果为:
明文: leave at zero in the night
密文: ohdyh dw chrq lq wkh qljkw
所以,加密后的密文为“ohdyh dw chrq lq wkh qljkw”
原文地址: https://www.cveoy.top/t/topic/fFI8 著作权归作者所有。请勿转载和采集!