文章目录
- 原理太简单就不赘述了!
- 一、凯撒加密(源码)
- 二、暴力破解凯撒加密(源码)
- 三、运行结果截图
原理太简单就不赘述了!
一、凯撒加密(源码)
plaintext = input("请输入明文(小写):\n")#输入明文
key = int(input("请输入密钥(0~25):\n"))#输入明文plaintext= str.upper(plaintext)#都换成大写
ciphertext=''for x in plaintext:#空格还输出空格,将输出默认换行去掉if(x==' '):ciphertext=ciphertext+' '#ord()函数返回对应字符的ascii码,chr()表示ascii码对应的字符#需要拐回头elif(ord(x)-ord('A')+key >= 26 ):ciphertext=ciphertext+chr(ord(x)-26+key)#不需要else:ciphertext=ciphertext+chr(ord(x)+key)#输出密文
print ("密文为:",ciphertext)
二、暴力破解凯撒加密(源码)
ciphertext = input('请输入密文(小写):\n ')
ciphertext = str.upper(ciphertext)print("所有可能的明文为:")
for key in range(0,26):#依次尝试26个密钥plaintext=''for x in ciphertext:if(x==' '):plaintext=plaintext+' 'elif(ord(x)-ord('A')-key<0):plaintext=plaintext+chr(ord(x)-key+26)else:plaintext=plaintext+chr(ord(x)-key)print ("(key=",key,"):",plaintext)
三、运行结果截图