Vigenere密码不工作 [英] Vigenere cipher not working

查看:219
本文介绍了Vigenere密码不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的老师创造了这个基因密码,他说这是正常的。
但是,在使用在线vigenere密码检查其结果后,似乎不会导致正确的加密。



我不知道如何解决它,我想知道有没有人可以指引我的错误,并告诉我如何解决这些错误。



这是代码




alphabets ='abcdefghijklmnopqrstuvwxyz'
keyword = input('什么是你的关键字')
message =输入('你的消息要编码或编码?)。lower()

expandedKeyword =
while len(expandedKeyword)< len(message):
for i in keyword:
if len(expandedKeyword)< len(message):
expandedKeyword + = i


cipheredMessage =''
indexofKeyword = 0
在我的消息中:
if i ==''
cipheredMessage = cipheredMessage +
else:
shifIndex =(ord(i)+ ord(expandedKeyword [indexofKeyword]) - base)%26 + base
cipheredMessage = cipheredMessage + chr(shifIndex)
indexofKeyword = indexofKeyword + 1



打印(cipheredMessage)

我了解发生了什么的概念,但我似乎无法弄清楚错误。

解决方案

您的 shifIndex 的计算错误,您需要减去 base 两次,但你目前只是减去一次。示例 -

  shifIndex =(ord(i)+ ord(expandedKeyword [indexofKeyword]) -  2 * base)%26 + base 

这是因为您需要减去 base 第一次从 ord(i)获取i的索引(从'a'),第二次从 ord(expandedKeyword [indexofKeyword]),获取该字符的索引(从'a')。所以它应该是(为了更好的理解) -

  shifIndex =((ord(i) -  base)+(ord扩展关键字[关键词索引]) - 基数))%26 + base 


So my teacher created this vigenere cipher and he said it was working. However, after checking its results with online vigenere ciphers it appears to not be resulting with the correct encryptions.

I have no idea how to fix it and I was wondering if someone could direct me to the errors, and tell me how to fix them.

Here is the code

base = ord("a")
alphabets = 'abcdefghijklmnopqrstuvwxyz'  
keyword = input('What is your keyword')
message = input('What is your message to be coded or encoded?').lower()

expandedKeyword = ""
while len(expandedKeyword) < len(message): 
    for i in keyword:
        if len(expandedKeyword) < len(message): 
            expandedKeyword += i 


cipheredMessage = '' 
indexofKeyword = 0
for i in message:
        if i == ' ':
            cipheredMessage = cipheredMessage + " "
        else:
            shiftedIndex = (ord(i) + ord(expandedKeyword[indexofKeyword])-base) % 26 +base
            cipheredMessage = cipheredMessage + chr(shiftedIndex)
            indexofKeyword = indexofKeyword + 1



print(cipheredMessage)

I understand the concept of what is happening, but I cant seem to figure out the error.

解决方案

Your calculation of shiftedIndex is wrong, you need to subtract base twice , but you are currently only subtracting it once. Example -

shiftedIndex = (ord(i) + ord(expandedKeyword[indexofKeyword])-2*base) % 26 +base

This is because you need to subtract base first time from ord(i) to get the index of i (from 'a') , and the second time from ord(expandedKeyword[indexofKeyword]) , to get the index of that character (from 'a' ) . So it should look like (for better understanding) -

shiftedIndex = ((ord(i) - base) + (ord(expandedKeyword[indexofKeyword])-base)) % 26 + base

这篇关于Vigenere密码不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆