Python转换与字母表

时间:2016-02-03 13:16:23

标签: python decode shift

def menu():
    choice = input("Press 1 to encode, 2 to decode, 9 to exit ")
    return choice

def makeKeycode(message):
    key = input("What is the key? ").upper()
    length = len(message)
    keycode = ""
    counter = 0
    while length >0:
        if counter == len(key):
            counter = 0
        keycode = keycode + key[counter]
        counter = counter + 1
        length = length - 1
    print(keycode)
    return keycode

def enterMessage():
    message = input("What is the message ").upper()
    return message

def encodeMessage(message, keycode):
    ciphertext =""
    alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
    for i in range (len(message)):
        character = message[i]
        charCode = alphabet.find(character)
        keycodeChar =keycode[i]
        keyLetter = alphabet.find(keycodeChar)
        position = (charCode + keyLetter)%25
        cipherLetter = alphabet[position]
        ciphertext =ciphertext + cipherLetter
    return ciphertext

def decodeMessage(ciphertext,keycode):
    ciphertext =""
    alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
    for i in range (len(ciphertext)):
        character = ciphertext[i]
        charCode = alphabet.find(character)
        keycodeChar =keycode[i]
        keyLetter = alphabet.find(keycodeChar)
        position = (charCode - keyLetter)%25
        cipherLetter = alphabet[position]
        ciphertext =ciphertext - cipherLetter
    return message

def enterCipher ():
    ciphertext = input("Enter the text to be decoded")
    return ciphertext

def encode():
    message = enterMessage()
    keycode = makeKeycode(message)
    ciphertext = encodeMessage(message,keycode)
    print(ciphertext)
def decode():

    keycode = makeKeycode(ciphertext)
    message = decodeMessage(ciphertext, keycode)
    print (message)   

def main():
    MyDictionary=("A:1","B:2","C:3","D:4","E:5","F:6","G:7","H:8","I:9","J:10","K:11","L:12","M:13","N:14","O:15","P:16","Q:17","R:18","S:19","T:20","U:21","V:22","W:23","X:24","Y:25","X:26")
    print (MyDictionary)
    choice = 0
    while choice !=9:
        choice = int(menu())

        if choice == 1:
            encode()
        elif choice == 2:
            decode()

if __name__ == "__main__":
    main()

嗨,我不能让我的代码工作,我的编码功能工作,但我正在努力修复我的解码功能。我不明白我哪里错了。我希望能够解码我将编码的消息,但这并不起作用,因为解码功能会停止程序。我在编码中犯了错误吗?谢谢你的帮助 此致

2 个答案:

答案 0 :(得分:3)

def decodeMessage(ciphertext,keycode):
    ciphertext =""

您似乎正在将密文重置为空字符串。结果,你的len(ciphertext)为0,这意味着你没有做任何事情。

python 2.75

sig = 'abcd'
def test(sig):
    sig = ""
    print 'sig = ', sig

>>> test(sig)
    sig = ''
>>> print sig
    'abcd'

答案 1 :(得分:0)

我发现解码消息功能存在两个问题:

def decodeMessage(ciphertext,keycode):
    ciphertext =""
    alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
    for i in range (len(ciphertext)):
        character = ciphertext[i]
        charCode = alphabet.find(character)
        keycodeChar =keycode[i]
        keyLetter = alphabet.find(keycodeChar)
        position = (charCode - keyLetter)%25
        cipherLetter = alphabet[position]
        ciphertext =ciphertext - cipherLetter
    return message

我发现的第一个问题是你将密文作为强制参数传递给decodeMessage,然后你将密文的值改为"" (一个空字符串)。我建议将参数中的密文更改为" ct"防止重叠。

我发现的第二个问题是您返回message,但您不会对邮件执行任何操作。运行此程序时,您将收到全局错误,因为message不是全局错误,但您在本地范围内调用message而未在本地定义它。

现在,另一件事:你的编码方法没有正确编码任何东西。请查看以下输出:

Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 18
YNLD
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 5
YNLD
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 1
YNLD

所有人都被编码到“YNLD”中。无论关键。要使用Caesar Cypher加密某些东西,你会想要使用ords和chrs。 Ord使字符数字化,Chr使数字成为一个字母。考虑Python 3中的以下函数,例如:

from string import ascii_letters
def cypher_letter(letter, key):
    if letter not in ascii_letters: #commas, question marks, etc
        return letter 
    if letter.islower(): #lowercase and uppercase have differing ord values
        lower, upper = ord('a'), ord('z') # values: 97 to 122 
    elif letter.isupper():
        lower, upper = ord('A'), ord('Z') # values: 65 to 90
    else:
        print("Something went wrong.") #probably didn't enter valid letters

    cypher = ord(letter) + key #if key is "1" and letter is 'a', 97 + 1
    if cypher < lower: cypher += 26 #26 letters in alphabet, this is for looping
    if cypher > upper: cypher -= 26 #back around if it exceeds upper,lower limits
    return chr(cypher) #chr(98) = 'b'

以下代码:

cyphered_words = []
keys = [18, 5, 1]
for key in keys:
    encoded = ""
    for letter in 'some':
        encoded += (cypher_letter(letter, key)) #to cypher we pass the key
    print(encoded)
    cyphered_word.append(encoded)

输出:

Kgewlzafy hjwllq dgfy, oalz ugehdwp dwllwjk!
Xtrjymnsl uwjyyd qtsl, bnym htruqjc qjyyjwx!
Tpnfuijoh qsfuuz mpoh, xjui dpnqmfy mfuufst!

Cyphering和decyphering几乎是一回事。这就像他们是阴阳之类的东西,所以decypher函数看起来像:

def decrypt_message(message, key):
    decyphered_text = ""
    for letter in message:
        decyphered_text += cypher_letter(letter, (key*-1)) #to decypher we pass the negative key 
    return decyphered_text

一个解密我们3&#39;的循环:

for count, word in enumerate(cyphered_words):
    print(decrypt_message_k(word, keys[count]))

输出:

Something pretty long, with complex letters!
Something pretty long, with complex letters!
Something pretty long, with complex letters!

我希望这可以帮助你走上正轨。