更改字符串加密程序的输出

时间:2016-09-21 01:54:18

标签: python-3.x encryption printing

我编写了这个程序,我想知道如何更改它,以便如果用户输入d来解密消息,它将要求消息然后是密钥,然后它将退出,因为它现在只是循环。

associations = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,:;'\"/\\<>(){}[]-=_+?!"

command = input("Enter e to encrypt, d to decrypt, or q to quit: ")

while command!="q":

if command=="e":
    message = input("Message: ")
    key = input("Key: ")
    mlist = []
    klist = []
    addlist = []

    for x in message:
        mlist.append(associations.find(x))
    for x in key:
        klist.append(associations.find(x))

    n = len(mlist)
    while n>0:
        y = klist[len(mlist)-n]
        klist.append(y)
        n=n-1

    ziplist = list(zip(klist, mlist))
    for x in ziplist:
        addlist.append(x[0] + x[1])

    for x in addlist:
        if x< len(associations):
            print(associations[x], end="")
        else:
            print(associations[x-len(associations)], end="")
    print( )



elif command=="d":
    message = input("Message: ")
    key = input("Key: ")
    dmlist = []
    klist = []
    addlist = []

    for x in message:
        dmlist.append(associations.find(x))
    for x in key:
        klist.append(associations.find(x))

    n = len(dmlist)
    while n>0:
        y = klist[len(dmlist)-n]
        klist.append(y)
        n=n-1

    ziplist = list(zip(klist, dmlist))
    for x in ziplist:
        addlist.append(x[1]-x[0])

    for x in addlist:
        if x< len(associations):
            print(associations[x], end="")
        else:
            print(associations[x-len(associations)], end="")
    print( )


else: 
    print("Did not understand command, try again.")

command = input("Enter e to encrypt, d to decrypt, or q to quit: ")

if command=="q":
    print("Goodbye!")

1 个答案:

答案 0 :(得分:0)

忽略变量名称

import math
x = ""
while x != "q":
    doge=input("Enter e to encrypt, d to decrypt, or q to quit: ")
    associations = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,:;'\"/\\<>(){}[]-=_+?!"
    fin=9000*associations
    if doge=="q":
        print("Goodbye!")
        break
    elif doge=="e":
        potato=input("Message: ")
        key=input("Key: ")
        a_list=[]
        b_list=[]
        for x in potato:
            a_list.append(associations.find(x))
        for x in key:
            b_list.append(associations.find(x))
        if len(a_list) > len(b_list):
            divide=len(a_list)/len(b_list)
            a=math.ceil(divide)
            bb_list=a*b_list
            ab_list=zip(a_list,bb_list)
            encrypt=[x+y for x, y in ab_list]
        else:
            ddivide=len(b_list)/len(a_list)
            a=math.ceil(ddivide)
            aa_list=a*a_list
            ab_list=zip(aa_list,b_list)
            encrypt=[x+y for x, y in ab_list]
        for x in encrypt:
            print(fin[x],end="")
        print("\n")
    elif doge=="d":
        POTATO=input("Message: ")
        KEY=input("Key: ")
        A_list=[]
        B_list=[]
        for x in POTATO:
            A_list.append(associations.find(x))
        for x in KEY:
            B_list.append(associations.find(x))
        if len(A_list) > len(B_list):
            DIVIDE=float(len(A_list)/len(B_list))
            a=math.ceil(DIVIDE)
            BB_list=B_list*a
            AB_list= zip(A_list, BB_list)
            decrypt=[x-y for x, y in AB_list]
        else:
            DDIVIDE=len(B_list)/len(A_list)
            a=math.ceil(DDIVIDE)
            AA_list=A_list*a
            AB_list= zip(AA_list,B_list)
            decrypt=[x-y for x, y in AB_list]
        for x in decrypt:
            print(fin[x],end="")
        print("\n")
    else:
        print("Did not understand command, try again.")