凯撒密码介绍
它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。
将其抽象成数学语言,可表示为:
def kaisa(plaintext=None,key=None):
ciphertext = ""
for item in plaintext: # 遍历所有字符英文字符,进行偏移,非英文字符不做任何处理
if item >= 'A' and item <= 'Z':
item = chr(ord('A') + ((ord(item) - ord('A') + key) % 26))
elif item >= 'a' and item <= 'z':
item = chr(ord('a') + ((ord(item) - ord('a') + key) % 26))
ciphertext += item
return ciphertext
if __name__ == "__main__":
plaintext = "Cryptographic experiments"
ciphertext = kaisa(plaintext=plaintext,key=23)
print("ciphertext : %s"%(ciphertext))
#ciphertext : Zovmqldoxmefz bumbofjbkqp