ASCII Vigenere密码未正确解密 [英] ASCII Vigenere cipher not decrypting properly
问题描述
对于加密:
encryption = [((x + y)%26)+ ord ')如果x < 128 else x for x,y in zip(msglist,keylist)]
解密:
encryption = [((x - y)%26)+ ord('A')如果x < 128 else x for x,y in zip(msglist,keylist)]
如果我输入Python加密密钥爱我得到:
GYAXLN
不幸的是,当我尝试使用相同的密钥解密GYAXLN时,我得到:
PEZNUT
哪些不是应该的。我认为我的数学有问题,但是我不能很好地得到它,直到现在已经尝试了不同的数字,看看什么有用(我不是最好的数学)。我在这里缺少什么?
问题在语句(x - y)%26
因为一个数字的mod定义为0到m-1,在上述情况下为0到25,但是减去你会得到负数,所以为了得到正确的结果你应该这样做(x - y + 26)%26
。如果我们在(xy)
中得到的负值低于-m,低于-26,那么否仍然是否定的,那么你必须使它是正面的,这里是伪代码:
val =(x - y)%26
while (val <0)val + = 26
val = val%26
My Vigenere cipher program has all come down to two lists. One a list of ASCII numbers which represent the characters of the message to be encrypted/decrypted and the other is a list of ASCII numbers of the key that would be used to decrypt/encrypt the message.
For encryption:
encryption = [((x + y) % 26) + ord('A') if x < 128 else x for x, y in zip(msglist, keylist)]
for decryption:
encryption = [((x - y) % 26) + ord('A') if x < 128 else x for x, y in zip(msglist, keylist)]
If I input 'Python' with the key 'love' to encrypt I get:
GYAXLN
Unfortunately, when I try to decrypt 'GYAXLN' using the same key I get:
PEZNUT
Which is not what it should be. I think there is something wrong with my math but I just can't quite get it right and up till now have been trying different numbers to see what works (I'm not the best at maths). What am I missing here? Can it not all be done in a list comprehension?
The problem is in the statement (x - y) % 26
because mod of a number is defined from 0 to m-1, in the above case 0 to 25, but while subtracting you get negative numbers, so in order to get the correct result you should do this (x - y + 26) % 26
. If the negative value we get by (x-y)
is lower than "-m" in the above case lower than -26, then the no still remains negative, then you have to make it positive, here is the pseudo code for that :
val = (x - y) % 26
while(val < 0) val += 26
val = val % 26
这篇关于ASCII Vigenere密码未正确解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!