Vigenere Cipher Python 给出外国字符而不是英语
Vigenere Cipher Python giving foreign characters instead of English
此代码旨在使用 Vigenere Cipher 技术进行加密和解密,当我选择加密并输入我的关键字和文本时,结果是一堆外来字符,一次一个打印在单独的行上,如下所示:
大号
罗
楼等。此外,当我选择解密时,打印结果是一个错误,指出 'print index is out of range'。这两个错误我都不知道如何解决,如果有任何帮助,我将不胜感激。
#encryption
def encrypt():
crypt = ''
key_phrase = raw_input("Please enter a key phrase to encrypt by: ")
key_text = raw_input("Please enter a piece of text to encrypt: ")
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 decrypt by: ")
_key_text = raw_input("Please enter a piece of text to include: ")
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_phrase[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
choice = raw_input("Please enter either 'a' for encryption or 'b' for decryption: ")
if choice == 'a':
print encrypt()
else:
print decrypt()
您使用大写的 ASCII 代码,但您的密钥和文本是小写的。
可能的解决方案:
输入时使用大写键和文本。
通过 key_phrase = key_phrase.upper()
将键和文本设置为大写
小写字母使用 ASCII 码,大写字母不使用。例如范围从 97 到 122,而不是 65 到 90。
这会起作用(尽管这不是最佳解决方案):
正在加密:
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()
解密中:
_key_phrase = raw_input("Please enter a key phrase to decrypt by: ")
_key_phrase = _key_phrase.upper()
_key_text = raw_input("Please enter a piece of text to include: ")
_key_text = _key_text.upper()
此代码旨在使用 Vigenere Cipher 技术进行加密和解密,当我选择加密并输入我的关键字和文本时,结果是一堆外来字符,一次一个打印在单独的行上,如下所示: 大号 罗 楼等。此外,当我选择解密时,打印结果是一个错误,指出 'print index is out of range'。这两个错误我都不知道如何解决,如果有任何帮助,我将不胜感激。
#encryption
def encrypt():
crypt = ''
key_phrase = raw_input("Please enter a key phrase to encrypt by: ")
key_text = raw_input("Please enter a piece of text to encrypt: ")
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 decrypt by: ")
_key_text = raw_input("Please enter a piece of text to include: ")
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_phrase[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
choice = raw_input("Please enter either 'a' for encryption or 'b' for decryption: ")
if choice == 'a':
print encrypt()
else:
print decrypt()
您使用大写的 ASCII 代码,但您的密钥和文本是小写的。
可能的解决方案:
输入时使用大写键和文本。
通过
key_phrase = key_phrase.upper()
将键和文本设置为大写
小写字母使用 ASCII 码,大写字母不使用。例如范围从 97 到 122,而不是 65 到 90。
这会起作用(尽管这不是最佳解决方案):
正在加密:
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()
解密中:
_key_phrase = raw_input("Please enter a key phrase to decrypt by: ")
_key_phrase = _key_phrase.upper()
_key_text = raw_input("Please enter a piece of text to include: ")
_key_text = _key_text.upper()