栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python实现凯撒加密和暴力破解凯撒加密(源码及运行结果截图)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python实现凯撒加密和暴力破解凯撒加密(源码及运行结果截图)

文章目录
    • 原理太简单就不赘述了!
  • 一、凯撒加密(源码)
  • 二、暴力破解凯撒加密(源码)
  • 三、运行结果截图


原理太简单就不赘述了! 一、凯撒加密(源码)
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)

三、运行结果截图


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/321232.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号