维格纳密码,如何处理超出字符值范围的序数值

2024-09-25 02:28:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个学生在做我的课程,我们必须作出一个维格纳密码程序。 我知道我的程序是复杂的,但我不知道有任何其他方式来做它和它来不及修复。 我似乎有一个问题,当我添加的序数值的消息和关键字的新的序数值是超出范围的正常字母值。所以它会打印出像这样奇怪的字母。你知道吗

这是我的密码:

newmessage1 = []
stringposition = 0
number1 = 0
mesletter = 0
keyletter = 0
output =[]
keylist = []
stringofnumbs = []
question = input('Would you like to encrypt, decrypt a message or quit?')
encdec=[]
if question == 'encrypt'or'encrypt a string'or'ENCRYPT'or'encrypt_a_message'or'Encrypt':
    message1 = input('Please input a message to encrypt')
    keyword1 = input('Please input a keyword ')
    if message1.isalpha and keyword1.isalpha:#this check if the message and the keyword only contains letters
        messagelength=len(message1)#this sets the variable 'messagelength' to the length of message1
        newkeyword=''#this is an empty variable used to insert the length of the new keyword
        while len(keyword1)<=len(message1):#this will loop checks that the length of keyword1 is smaller thasn the length of message1.
            keyword1+=keyword1#this will repeat the keyword to fit the length of the message
            newkeyword=keyword1[:len(message1)]
            #this set the 'newkeyword' variable to the length of the new keyword
            #(the new keyword is the keyword repeated to fit the length of the message.) 

            for mesletter in message1:
                mesnumber = ord(mesletter)#what it does is it takes every letter in message1 and puts it into its unicode form.
                stringofnumbs.append(mesnumber)#once one letter has been put in its unicode value it will append that unicdoe from of that
                #letter in the variable 'stringofnumbs',it will do this process for every letter in the message. 

            for keyletter in keyword1:
                keynumber = ord(keyletter)#what it does is it takes every letter in keyword1 and puts it into its unicode form.
                keylist.append(keynumber)#once one letter has been put in its unicode value it will
                #append that unicdoe from of that letter in the variable 'stringofnumbs',it will do this process for every letter in the message.

                temp1 = int(stringofnumbs[stringposition])#temp1 is a variable that holds the ordinal values of letters in message1
                temp2 = int(keylist[stringposition-1])#and temp2 is a variable that holds the ordinal values of letters in the keyword1
                temp3 = temp2+temp1#temp3 then adds these ordinal values togther 
                encdec.append(temp3)#the ordinal values added togther are appended to the encdec variable

            for number1 in encdec:
                newletter1=chr(number1)#this for loop takes every letter in encdec and puts the ordinal values back into charcters
                newmessage1.append(newletter1)#then every letter that has been changed to a charcater value is appended to the varibale newmessage1
            print (' ')#this leaves a bit of space to make the encoded text clear    
            print ('the endcoded text is:')#this is just a bit of text to make the encoded text clear to the user
            print (''.join(newmessage1))#here the encoded message is printed
    else:
        ('The message or keyword is invalid please use only numbers')#this will print only if the keyword or message doesnt only contain letters and spaces

Tags: orofthetoinmessagethatis
2条回答

你用序数使事情变得不必要的复杂化。实际上,你使用的“字母表”有256个符号,而你只对26个感兴趣。你知道吗

理想情况下,你只需要你的符号在0到25之间的数字范围内,一旦你达到26,你会再次扭曲到0。你可以用序数来实现这一点,但是你必须使用正确的偏移量。你知道吗

现在,假设您只使用大写字母。它们的序数从65到90不等。所以一旦你到了91,你就想转到65。你知道吗

if temp3 > 90:
    temp3 -= 26

另一个问题是您只需添加temp1temp2temp1可以保持为介于65和90之间的数字,但是temp2应该是键偏移量,范围从0到25。因此,当您计算temp2时,您真的应该这样做

temp2 = int(keylist[stringposition-1]) - 65

这将从概念上解决您的问题,但您的算法仍然不正确。while循环的目的是将关键字的长度至少扩展为消息的长度。其他所有内容必须在左边缩进4个空格,这样它们就不是循环的一部分。明确地说

while len(keyword1)<=len(message1):
    keyword1+=keyword1
newkeyword=keyword1[:len(message1)]

for mesletter in message1:
    keynumber = ord(keyletter)
    stringofnumbs.append(mesnumber)

for keyletter in newkeyword:
    keynumber = ord(keyletter)
    keylist.append(keynumber)

    temp1 = int(stringofnumbs[stringposition])
    temp2 = int(keylist[stringposition]) - 65
    temp3 = temp2+temp1
    if temp3 > 90:
        temp3 -= 26
    encdec.append(temp3)

    stringposition += 1

etc

在上面的代码块中,我想指出我纠正的更多错误。你应该迭代newkeyword,而不是keyword1,而且你还必须增加每个字母的stringposition计数器。你知道吗


对于一个更有效、更不复杂的算法,可以考虑将代码发布到Code Review,以便进行回顾,并从得到的反馈中学习。你知道吗

虽然不是编写代码的最佳方法,但在保持代码不变的同时,仍然可以进行一些基本的整理。基本上,一旦你的消息和关键字的长度相同,你就可以遍历这对字母,然后计算加密的字母,这样就可以把它存储在一个列表中。没有理由存储中间步骤,例如将消息和关键字转换为数字列表。你知道吗

if message1.isalpha() and keyword1.isalpha():
    message1 = message1.upper()
    keyword1 = keyword1.upper()

    if len(keyword1) < len(message1):
        keyword1 = keyword1 * (len(message1) / len(keyword1)) + keyword1
    keyword1 = keyword1[:len(message1)]

    for m, k in zip(message1, keyword1):
        encrypted = ord(m) + (ord(k) - 65)
        if encrypted > 90:
            encrypted -= 26
        encdec.append(chr(encrypted))

    print ('\nThe endcoded text is:')
    print (''.join(encdec))

我认为解决这个问题最常见的方法是把数字再“包装”一下。也就是说,如果除以要允许的最大有效序数,然后使用余数。你知道吗

例如,使用chr作为ord的倒数:

In [0]: chr(122)    # I guess this is the last valid character you want?
Out[0]: 'z'

In [1]: chr(123)    # Because now we go into symbols
Out[1]: '{'

In [2]: 234%122    # We can take the remainder using modulo division
Out[2]: 112

In [3]: chr(234)    # Not what you want?
Out[3]: 'ê'

In [4]: chr(234%122)    # What you want?
Out[4]: 'p'

In [5]: chr(0) # On further inspection, you also need to add a constant when you divide
Out[5]: '\x00'

In [6]: chr(49)    # Try adding 49 after modulo dividing, and that should keep you alphanumeric.
Out[6]: '1'

我真的不知道这会影响你以后的描述,我还没有调查过,但你可能会想一想。你知道吗

相关问题 更多 >