1. 程式人生 > >凱撒密碼解密指令碼(python)

凱撒密碼解密指令碼(python)

def casearDecrypt(ciphertext, source_char, destination_char): 
    
    offset = ord(destination_char) - ord(source_char)
    chars = "abcdefghijklmnopqrstuvwxyz"
    
    for char in ciphertext:
        
        is_upper_flag = 0
        if char.isupper():
            char = char.lower()
            is_upper_flag = 1
        
        if char not in chars:
            outputChar(is_upper_flag, char)
            continue
        
        tempchar_ascii = ord(char) + offset
        tempchar =chr(tempchar_ascii)
        if tempchar not in chars:
            if offset < 0:
                tempchar_ascii += len(chars)
            else:
                tempchar_ascii -= len(chars)
        tempchar = chr(tempchar_ascii)
        outputChar(is_upper_flag, tempchar)

def outputChar(is_upper_flag, char):
    
    if is_upper_flag == 1:
        print(char.upper(), end="")
    else:
        print(char, end="")

ciphertext = input("Please input ciphertext:\n")
des_char = input("Please input destination_char:\n")
sors_char = input("Please input source_char:\n")
casearDecrypt(ciphertext, sors_char, des_char)

上面的指令碼只能用於已知一個密文字母和明文字母相對應的情況。如果想要列舉所有情況,就不可以了,所以又重新編寫改進,加入了列舉所有偏移的選項。改進後的程式碼如下:

def casearDecrypt(ciphertext, source_char, destination_char, list_all): 
    
    if list_all == True:
        for offset in range(1, 27):
            convertChar(offset)
    else:
        offset =  ord(destination_char) - ord(source_char)
        convertChar(offset)

def convertChar(offset):
    chars = "abcdefghijklmnopqrstuvwxyz"
    for char in ciphertext:
         
        is_upper_flag = 0
        if char.isupper():
            char = char.lower()
            is_upper_flag = 1
         
        if char not in chars:
            outputChar(is_upper_flag, char)
            continue
         
        tempchar_ascii = ord(char) + offset
        tempchar =chr(tempchar_ascii)
        if tempchar not in chars:
            if offset < 0:
                tempchar_ascii += len(chars)
            else:
                tempchar_ascii -= len(chars)
        tempchar = chr(tempchar_ascii)
        outputChar(is_upper_flag, tempchar)
    print("")

def outputChar(is_upper_flag, char):
    
    if is_upper_flag == 1:
        print(char.upper(), end="")
    else:
        print(char, end="")

ciphertext = input("Please input ciphertext:\n")
while True:
    operation = input("List all results?y/n:")
    if operation == 'y' or operation == 'Y':
        casearDecrypt(ciphertext, '', '', True)
        break
    elif operation == 'n' or operation == 'N':
        des_char = input("Please input destination_char:\n")
        sors_char = input("Please input source_char:\n")
        casearDecrypt(ciphertext, sors_char, des_char, False)
        break
    else:
        print("Input error! Please input y/n:")