Vigenere 密码 'string index out of range' 用于解密
Vigenere cipher 'string index out of range' for decryption
我的 Vigenere 密码非常适合加密,但我只需要修复这个解密问题,我被告知,在 运行 程序之后,字符串索引超出范围。有人可以让我知道我需要将其更改为什么吗,如果可以的话,我将不胜感激。
编辑:我已经更改了导致字符串索引问题的代码部分,但现在,在处理解密时,输出是一个空行,下面有 'None',没有错误。
edit: ord(_key_text[letters%len(_key_text)]) 我需要在 decrypt 中用 _key_phrase 替换方程这一边的 _key_text .
#encryption
def encrypt():
crypt = ''
key_phrase = raw_input("Please enter a key phrase to encrypt by: ")
key_phrase = key_phrase.upper()
key_text = raw_input("Please enter a piece of text to encrypt: ")
key_text = key_text.upper()
if len(key_text) == 0:
print("Key must be of length 1 or more."); exit()
if not key_text.isalpha() or not key_phrase.isalpha():
print("Both text and key must be composed of letters only."); exit()
for letters in range(0, len(key_text)):
new = ord(key_text[letters]) + ord(key_text[letters%len(key_text)]) - 65
if new > 90:
new -= 26
crypt += chr(new)
print crypt
#decryption
def decrypt():
decrypt = ''
_key_phrase = raw_input("Please enter a key phrase to encrypt by: ")
_key_phrase = _key_phrase.upper()
_key_text = raw_input("Please enter a piece of text to encrypt: ")
_key_text = _key_text.upper()
if len(_key_text) == 0:
print("Key must be of length 1 or more."); exit()
if not _key_text.isalpha() or not _key_phrase.isalpha():
print("Both text and key must be composed of letters only."); exit()
for letters in range(0, len(_key_text)):
new = ord(_key_text[letters]) - ord(_key_text[letters%len(_key_text)]) + 65
if new < 65:
new += 26
decrypt == chr(new)
print decrypt
#asking the user to enter a or b for en/decryption and whether they wish to continue
choice = raw_input("Please enter either 'a' for encryption or 'b' for decryption: ")
if choice == 'a':
print encrypt()
else:
print decrypt()
如果您的关键文本比关键短语长,您将在此处获得无效索引:
new = ord(_key_phrase[letters]) - ord(_key_text[letters%len(_key_text)])
当 letters
变得大于 len(_key_text)
你在 encryption
和 key text
和 key phrase
的两个地方都有 key_text
解密,所以你必须在这里改变一个变量。
我的 Vigenere 密码非常适合加密,但我只需要修复这个解密问题,我被告知,在 运行 程序之后,字符串索引超出范围。有人可以让我知道我需要将其更改为什么吗,如果可以的话,我将不胜感激。
编辑:我已经更改了导致字符串索引问题的代码部分,但现在,在处理解密时,输出是一个空行,下面有 'None',没有错误。
edit: ord(_key_text[letters%len(_key_text)]) 我需要在 decrypt 中用 _key_phrase 替换方程这一边的 _key_text .
#encryption
def encrypt():
crypt = ''
key_phrase = raw_input("Please enter a key phrase to encrypt by: ")
key_phrase = key_phrase.upper()
key_text = raw_input("Please enter a piece of text to encrypt: ")
key_text = key_text.upper()
if len(key_text) == 0:
print("Key must be of length 1 or more."); exit()
if not key_text.isalpha() or not key_phrase.isalpha():
print("Both text and key must be composed of letters only."); exit()
for letters in range(0, len(key_text)):
new = ord(key_text[letters]) + ord(key_text[letters%len(key_text)]) - 65
if new > 90:
new -= 26
crypt += chr(new)
print crypt
#decryption
def decrypt():
decrypt = ''
_key_phrase = raw_input("Please enter a key phrase to encrypt by: ")
_key_phrase = _key_phrase.upper()
_key_text = raw_input("Please enter a piece of text to encrypt: ")
_key_text = _key_text.upper()
if len(_key_text) == 0:
print("Key must be of length 1 or more."); exit()
if not _key_text.isalpha() or not _key_phrase.isalpha():
print("Both text and key must be composed of letters only."); exit()
for letters in range(0, len(_key_text)):
new = ord(_key_text[letters]) - ord(_key_text[letters%len(_key_text)]) + 65
if new < 65:
new += 26
decrypt == chr(new)
print decrypt
#asking the user to enter a or b for en/decryption and whether they wish to continue
choice = raw_input("Please enter either 'a' for encryption or 'b' for decryption: ")
if choice == 'a':
print encrypt()
else:
print decrypt()
如果您的关键文本比关键短语长,您将在此处获得无效索引:
new = ord(_key_phrase[letters]) - ord(_key_text[letters%len(_key_text)])
当 letters
变得大于 len(_key_text)
你在 encryption
和 key text
和 key phrase
的两个地方都有 key_text
解密,所以你必须在这里改变一个变量。