云安全-Python实现凯撒密码和替换密码的加密解密与暴力破解

记录顺带保存一次课堂作业,部分参考了现有的实现,结合需求进行了一波码,能用就行,人和代码能跑一个就好,替换密码暴力破解实在懒得搞了,或许以后会更?

import getopt
import sys

#接收参数,规定参数名
opts, args = getopt.getopt(sys.argv[1:], '-h-v-m:-a:-p:-c:-k:-A:', ['help', 'version', 'mode=', 'action=', 'plaintext=', 'ciphertext=', 'key=', 'admin='])
for opt_name, opt_value in opts:
    if opt_name in ('-h', '--help'):
        print("Help Info: \n"
              "-m or --mode, value can use caesar or permute \n"
              "-a or --action, value can use encode or decode or bruteAttack \n"
              "-p or --plaintext \n"
              "-c or --ciphertext \n"
              "-k or --key \n"
              "example-1:-m caesar -a encode -p helloworld -k 3 \n"
              "example-2:--mode permute --action=decode --ciphertext=helloworld -k word \n")
        sys.exit()

    elif opt_name in ('-v', '--version'):
        print("Version is 1.0, Update in 2022/4/26 ")
        sys.exit()

    elif opt_name in ('-m', '--mode'):
        mode = opt_value

    elif opt_name in ('-a', '--action'):
        action = opt_value

    elif opt_name in ('-p', '--plaintext'):
        plaintext = opt_value

    elif opt_name in ('-c', '--ciphertext'):
        ciphertext = opt_value

    elif opt_name in ('-k', '--key'):
        key = opt_value

    elif opt_name in ('-A', '--admin'):
        if(opt_value == "admin"):
            print("Developer:SZPT-AI")
            print("Mode =", mode)
            print("Action =", action)

#凯撒加密
def caesarEncode(plaintext, key):
    encodeText = []

    for word in plaintext:
        encodeText.append(chr(ord('a') + ((ord(word) - ord('a')) + int(key)) % 26))
    ciphertext = "".join(encodeText)

    print(ciphertext)

#凯撒解密
def caesarDecode(ciphertext, key):
    DecodeText = []

    for word in ciphertext:
        DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))
    plaintext = "".join(DecodeText)

    print(plaintext)

#凯撒暴力破解
def caesarBruteAttack(ciphertext):

    #遍历所有偏移量
    for key in range(1,26):
        DecodeText = []

        for word in ciphertext:
            DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))

        plaintext = "".join(DecodeText)
        print(plaintext)

def keyIntFormat(key):
    # 将key转化为数字
    tmp_key1 = []
    for keyword in key:
        tmp_key1.append(ord(keyword) - 96)

    # 建立临时列表进行排序
    tmp_key2 = sorted(tmp_key1)
    tmp_key = [] * len(key)

    for num in range(len(tmp_key2)):
        for keynum in range(len(tmp_key1)):
                if (tmp_key1[num] == tmp_key2[keynum]):
                    tmp_key.append(str(keynum + 1))
    key = "".join(tmp_key)
    
    return key

#置换加密
def permuteEncode(plaintext, key):
    keylen = len(key)
    ciphertext = ""

    key = keyIntFormat(key)

    #根据key长度对明文进行分段
    for part in range(0, len(plaintext), keylen):
        tmp_ciphertext = [""] * keylen

        #超长度内容处理
        if part + keylen > len(plaintext):
            tmp_plaintext = plaintext[part:]

        #内容处理
        else:
            tmp_plaintext = plaintext[part:part + keylen]

        for word in range(len(tmp_plaintext)):
            tmp_ciphertext[int(key[word]) - 1] = tmp_plaintext[word]
        ciphertext += "".join(tmp_ciphertext)

    print(ciphertext)


#置换解密
def permuteDecode(ciphertext, key):
    keylen = len(key)
    plaintext = ""

    key = keyIntFormat(key)

    #根据key长度对密文进行分段
    for part in range(0, len(ciphertext), keylen):
        #构筑矩阵
        tmp_plaintext = [""] * keylen

        #超长度内容处理
        if part + keylen >= len(ciphertext):
            tmp_ciphertext = ciphertext[part:]
            re_key = []

            #根据key确认位置
            for word in range(len(tmp_ciphertext)):
                re_key.append(int(key[word]) - 1)
            re_key.sort()

            for word in range(len(tmp_ciphertext)):
                tmp_plaintext[word] = tmp_ciphertext[re_key.index(int(key[word]) - 1)]

        #内容处理
        else:
            #分段内容
            tmp_ciphertext = ciphertext[part:part + keylen]

            for word in range(len(tmp_ciphertext)):
                    tmp_plaintext[word] = tmp_ciphertext[int(key[word]) - 1]
        plaintext += "".join(tmp_plaintext)

    print(plaintext)

#置换暴力破解
def permuteBruteAttack(ciphertext):
    #其实就是太麻烦了我不想写
    #此处思路可以根据出现频率高的单词字母去匹配字典有可能的日常高频单词(就是随缘撞库)
    print("Develop in future release")

if(mode == "caesar"):   #凯撒加密

    if(action == "encode"):
        caesarEncode(plaintext, key)

    elif(action == "decode"):
        caesarDecode(ciphertext, key)

    elif(action == "bruteAttack"):
        caesarBruteAttack(ciphertext)

elif(mode == "permute"):    #置换加密

    if(action == "encode"):
        permuteEncode(plaintext, key)

    elif(action == "decode"):
        permuteDecode(ciphertext, key)

    elif(action == "bruteAttack"):
        permuteBruteAttack(ciphertext)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值