美文网首页
凯撒加密

凯撒加密

作者: 颜承一 | 来源:发表于2024-04-11 19:58 被阅读0次
    input("明文输入:")
    set = input
    def kaisa ( text , k ):
        # 定义函数,用于实现凯撒加(解)密。
    
        ls = string.ascii_lowercase[k:] + string.ascii_lowercase[:k]
        us = string.ascii_uppercase[k:] + string.ascii_uppercase[:k]
        table = "". maketrans(string.ascii_letters , ls + us)
        return(text . translate(table))
    
    un_set = kaisa(set , 5)
    print("密文:" ,un_set , -5 )
    print("还原:" ,kaisa(un_sen , -5))
    commwords = ("the" , "is" , "to" , "not" , "if" , "iphone")
        # 常用单词列表commwords可以扩展,测试出现的单词次数也可以调整。
        # 将某段密文进行测试,分别测试密码是1,2,3……25。
        # 如测试密码5时,发现最常用的简单单词出现两个以上,就停止测试。
    
    for i in range(1,26);
        # 密钥取值范围限定在字母个数内。
    
        s = 0
        text = kaisa(un_set , -i)
        for word in commwords:
            # 遍历常用单词。
    
            if word in text:
                #如果常用单词在解密后的字符串里出现过。
    
            s + = 1
        if s >= 2
            # 常用单词出现次数超过二
            print(test)
            print("密钥:" ,i)
            break
    

    总感觉有哪里写错了。
    (思索)
    下次改一下。

    相关文章

      网友评论

          本文标题:凯撒加密

          本文链接:https://www.haomeiwen.com/subject/mghhxjtx.html