我正在编写一个python Vigenere密码,我遇到了一个错误“内存错误”。我不确定它是什么以及它为什么会发生,并且解密代码与加密代码相同(它确实可以加密但不能解密)。
playing = True
string = ""
Alphabet = ('z','a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
def EncryptVigenere():
plainText = input("Please enter the plain text: ")
key = input("Please enter the key: ")
keyList = []
keyLength = 0
num = 0
while keyLength < len(plainText):
try:
char = key[num]
except IndexError:
num = 0
char = key[num]
keyList.append(str(char))
keyLength += 1
num+= 1
CipherText = []
IndexValue = 0
keyIncrement = 0
for plainTextChar in plainText:
IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
while IndexValue > 26:
IndexValue = IndexValue - 26
CipherText.append(Alphabet[IndexValue])
keyIncrement = keyIncrement + 1
print (''.join(CipherText))
finish = input('Would you like to go again Y or N')
if finish == 'n' or finish == 'N':
retry = input ("Would you like to go again? Y or N: ")
if retry == 'N' or retry == 'n':
print ("Please exit the window")
import time
time.sleep(1)
import sys
sys.exit()
def DecryptVigenere():
plainText = input("Please enter the plain text: ")
key = input("Please enter the key: ")
keyList = []
keyLength = 0
num = 0
while keyLength < len(plainText):
try:
char = key[num]
except IndexError:
num = 0
char = key[num]
keyList.append(str(char))
keyLength -= 1
num -= 1
CipherText = []
IndexValue = 0
keyIncrement = 0
for plainTextChar in plainText:
IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
while IndexValue > 26:
IndexValue = IndexValue - 26
CipherText.append(Alphabet[IndexValue])
keyIncrement = keyIncrement - 1
print (''.join(CipherText))
finish = input('Would you like to go again Y or N')
if finish == 'n' or finish == 'N':
retry = input ("Would you like to go again? Y or N: ")
if retry == 'N' or retry == 'n':
print ("Please exit the window")
import time
time.sleep(1)
import sys
sys.exit()
while playing == True:
string = ""
eord = input('Type "d" to "decrypt" and "e" to "encrypt": ')
if eord == 'e':
texte = input ("Type your word to encrypt: ")
key1 = int(input("Choose a key between 1-26: "))
for letter in texte:
number = (ord(letter)) + (key1)
letter=(chr(number))
string = (str(string)) + (str(letter))
print (string)
keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")
if keyword == 'encrypt' or keyword == 'e':
EncryptVigenere()
elif keyword == 'decrypt' or keyword == 'd':
DecryptVigenere()
elif eord == 'd':
texd = input ("Type your word to decrypt: ")
key2 = int(input("Choose a key between 1-16: "))
for letter in texd:
number = (ord(letter)) - (key2)
letter=(chr(number))
string = (str(string)) + (str(letter))
print (string)
keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")
if keyword == 'decrypt' or keyword == 'd':
DecryptVigenere()
elif keyword == 'encrypt' or keyword == 'e':
EncryptVigenere()
以下是运行代码后出现的错误:
Type "d" to "decrypt" and "e" to "encrypt": e
Type your word to encrypt: hello
Choose a key between 1-26: 3
khoor
Type 'encrypt' code further or 'decrypt' further: e
Please enter the plain text: darrien
Please enter the key: brit
f
fs
fsa
fsal
fsalk
fsalkw
fsalkww
Would you like to go again Y or Ny
Type "d" to "decrypt" and "e" to "encrypt": d
Type your word to decrypt: khoor
Choose a key between 1-16: 3
hello
Type 'encrypt' code further or 'decrypt' further: d
Please enter the plain text: fsalkww
Please enter the key: brit
Traceback (most recent call last):
File "D:\##Python Cipher##2.py", line 111, in <module>
DecryptVigenere()
File "D:\##Python Cipher##2.py", line 54, in DecryptVigenere
keyList.append(str(char))
MemoryError
有谁知道导致错误的原因是什么? 还有办法缩短代码吗? 感谢。