凯撒和栅栏加密联合(python环境)

下面是实验的代码

import sys
password=[]
dspassword=[]
def ciper(plain,value):
    times=0
    secret_list=list(plain)
    # print(type(secret_list))
    secret_list_len=len(secret_list)
    try:
        value=int(value)
    except ValueError:
        print("Please input an integer.")
        sys.exit()


    #a的ANSI码是97, z的ANSI码是122。
    #A的ANSI码是65, Z的ANSI码是90。

    print("")
    print("Caesar cipher is : ",end='')

    while times < secret_list_len:
        times=times+1
        #ansi_raw即没有经过任何处理的原始ANSI。
        ansi_raw=ord(secret_list[0+times-1])

        #ansi是经过移位加密的ANSI。
        ansi=ansi_raw+int(value)

        #word是用户输入的原始字符。
        word=(secret_list[0+times-1])

        #如果ansi_raw小于65或大于90,而且还不是小写字母,那么则说明它根本就不是字母。不加密,直接输出原始内容。
        if (ansi_raw < 65 or ansi_raw > 90) and word.islower() == False :
            print(word,end='')

        #如果ansi_raw小于97或大于122,而且还不是大写字母,那么则说明它根本不是字母。不加密,直接输出原始内容。
        elif (ansi_raw < 97 or ansi_raw > 122) and word.isupper() == False:
            print(word,end='')

        #否则,它就是字母。
        else:
            #如果它是大写字母,而且ANSI码大于90,则说明向后出界。那么通过这个公式回到开头,直到不出界为止。
            while word.isupper() == True and ansi > 90:
                ansi = -26 + ansi 

            #如果它是大写字母,而且ANSI码小于65,则说明向前出界。那么通过这个公式回到结尾,直到不出界为止。
            while word.isupper() == True and ansi < 65:
                ansi = 26 + ansi

            #如果它是小写字母,而且ANSI码大于122,则说明向后出界。那么通过这个公式回到开头,直到不出界为止。
            while word.isupper() == False and ansi > 122:
                ansi = -26 + ansi

            #如果它是小写字母,而且ANSI码小于97,则说明向前出界。那么通过这个公式回到结尾,直到不出界为止。
            while word.isupper() == False and ansi < 97:
                ansi = 26 + ansi

            #将处理过的ANSI转换为字符,来输出密文。
            print(chr(ansi),end='')
            password.append(chr(ansi))

    print("--------------")
    # print(type(password))
    return password



def kaisa_diciper(ciphertext):

    ciphertext_list=list(ciphertext)
    # print(type(ciphertext_list))
    secret_list_len=len(ciphertext_list)
    for value in range(26):
        ds_pass=[]
        times=0
        try:
            value=int(value)
        except ValueError:
            print("Please input an integer.")
            sys.exit()


        #a的ANSI码是97, z的ANSI码是122。
        #A的ANSI码是65, Z的ANSI码是90。

        print("")
        print("Decryption of Kaiser is : ",end='')

        while times < secret_list_len:
            times=times+1
            #ansi_raw即没有经过任何处理的原始ANSI。
            ansi_raw=ord(ciphertext_list[0+times-1])

            #ansi是经过移位加密的ANSI。
            ansi=ansi_raw+int(value)

            #word是用户输入的原始字符。
            word=(ciphertext_list[0+times-1])

            #如果ansi_raw小于65或大于90,而且还不是小写字母,那么则说明它根本就不是字母。不加密,直接输出原始内容。
            if (ansi_raw < 65 or ansi_raw > 90) and word.islower() == False :
                print(word,end='')

            #如果ansi_raw小于97或大于122,而且还不是大写字母,那么则说明它根本不是字母。不加密,直接输出原始内容。
            elif (ansi_raw < 97 or ansi_raw > 122) and word.isupper() == False:
                print(word,end='')

            #否则,它就是字母。
            else:
                #如果它是大写字母,而且ANSI码大于90,则说明向后出界。那么通过这个公式回到开头,直到不出界为止。
                while word.isupper() == True and ansi > 90:
                    ansi = -26 + ansi 

                #如果它是大写字母,而且ANSI码小于65,则说明向前出界。那么通过这个公式回到结尾,直到不出界为止。
                while word.isupper() == True and ansi < 65:
                    ansi = 26 + ansi

                #如果它是小写字母,而且ANSI码大于122,则说明向后出界。那么通过这个公式回到开头,直到不出界为止。
                while word.isupper() == False and ansi > 122:
                    ansi = -26 + ansi

                #如果它是小写字母,而且ANSI码小于97,则说明向前出界。那么通过这个公式回到结尾,直到不出界为止。
                while word.isupper() == False and ansi < 97:
                    ansi = 26 + ansi

                #将处理过的ANSI转换为字符,来输出密文。
                print(chr(ansi),end='')
                ds_pass.append(chr(ansi))

        print("--------------")
        # print(type(ds_pass))
        dspassword.append(ds_pass)
    # print(dspassword)
    return dspassword
#----------------------------------栅栏加密算法--------------------------
def encrypt(encry_str):
    str_a=[]
    str_b=[]
    encry_str=list(encry_str)
    length=len(encry_str)
    if (length%2==0):
        for i in range(0,length,2):
            print(i)
            str_a.append(encry_str[i])
            str_b.append(encry_str[i+1])
        encry_str="".join(str_a+str_b)
        print(encry_str)
    else:
        for i in range(0,length):
            if(i%2==0):
                str_a.append(encry_str[i])
                # print(encry_str[i])
            else:
                str_b.append(encry_str[i])
        encry_str="".join(str_a+str_b)
        print("加密之后为"+encry_str)

def decipher(dec_str):
    dec_str_len=len(dec_str)
    dec_str=list(dec_str)
    field=[]
    if ((dec_str_len%2)==0):
        space_len=(dec_str_len//2)
        for i in range(space_len):
            field.append(dec_str[i])
            field.append(dec_str[i+space_len])
    else:
        space_len=(dec_str_len//2)+1
        for i in range(space_len):
            field.append(dec_str[i])
            if(i<(space_len-1)):
#                 print(i+space_len)
                field.append(dec_str[i+space_len])  

    print("栅栏解密后为:")
    print(field)
    return field

ok="y"
while ok=="y":
    choice=input("If you want to encrypt the string, please input 1\nIf you want to decrypt the string, please input anything\n")
    if choice=="1":
        plain=input("Please input your plain text: \n")
        value=input("Please input your key(included negatives): \n")
        encry_str=ciper(plain,value)
        encrypt(encry_str)
    else :
        dec_str=input("Please input your ciphertext: \n")
        ciphertext=decipher(dec_str)
        kaisa_diciper(ciphertext)

    ok=input("If you continue, please enter y\n\n")


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值