我正在使用Vingere加密密码来加密用户输入的字和密钥的程序。键可以是任意长度的,并包含重复的字母。用户输入单词的每个字母都使用与其下的关键字的字母对应的表行进行加密。加密在下面的表格中使用:

我已经开始编写代码,但我不知道如何将加密部分添加到我的代码中。到目前为止,这是一个项目:
import string
phrase = input("Please enter the phrase to be encrypted: ") #prompt user for phrase
key = input("Please enter the key: ") #prompt user for encryption key
# This code returns the encrypted text generated with the help of the key
key = list(key)
if len(phrase) == len(key):
print(key)
else:
for i in range(len(phrase) -
len(key)):
key.append(key[i % len(key)])
phrase = phrase.replace(" ", "")\
.translate(str.maketrans('', '', phrase.punctuation))\
.translate(str.maketrans('','',phrase.digits))\
.upper()
print("" . join(key))
cipher_text = []
for i in range(len(phrase)):
x = (ord(phrase[i]) +
ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
print("" . join(cipher_text))
print("Your word has been successfully encrypted!")
print("Original word: ", phrase)
print("Encryption: ", cipher_text)从现在开始我不知道该怎么办。我将试图找出如何获得加密代码,但我需要帮助,使程序加密用户输入和密钥使用Vigenere密码。如果有人能帮忙我会很感激的。请为您提供的任何输入显示代码的更改。提前感谢!
发布于 2022-01-25 17:39:33
我觉得你们很亲密。为了使密码正常工作,还需要将密钥大写为
key = key.upper()否则,在计算过程中:
x = (ord(phrase[i]) +
ord(key[i])) % 26您将添加一个与小写字符(key[i])的unicode相关联的数字,而不是大写字符。
换句话说,phrase[i]和key[i]的数字都应该引用大写字符,这样计算才能工作。
在进行此更改后,我使用短语"hello"和关键的"richard"获得以下输出
Encryption: ['Y', 'M', 'N', 'S', 'O']这与图表是一致的(进一步见下文)。
此外,短语被清除的部分应该是:
phrase = phrase.replace(" ", "")\
.translate(str.maketrans('', '', string.punctuation))\
.translate(str.maketrans('','',string.digits))\
.upper()我拥有的完整代码如下:
import string
phrase = input("Please enter the phrase to be encrypted: ") #prompt user for phrase
key = input("Please enter the key: ") #prompt user for encryption key
key = key.upper()
# This code returns the encrypted text generated with the help of the key
key = list(key)
if len(phrase) == len(key):
print(key)
else:
for i in range(len(phrase) -
len(key)):
key.append(key[i % len(key)])
phrase = phrase.replace(" ", "")\
.translate(str.maketrans('', '', string.punctuation))\
.translate(str.maketrans('','',string.digits))\
.upper()
print("" . join(key))
cipher_text = []
for i in range(len(phrase)):
x = (ord(phrase[i]) +
ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
print("".join(cipher_text))
print("Your word has been successfully encrypted!")
print("Original word: ", phrase)
print("Encryption: ", cipher_text)为了更明确地说明加密的工作原理,下面是一个图像,显示了在我的示例中每个单词是如何排列的:

绿色圆圈根据密码表示加密的结果。它与Encryption: ['Y', 'M', 'N', 'S', 'O']的输出结果相匹配。
https://stackoverflow.com/questions/70842811
复制相似问题