【发布时间】:2020-02-04 00:30:15
【问题描述】:
我使用 Vigenére 密码制作了一个基于菜单的加密工具。截至目前该程序加密空格,我怎样才能让程序跳过空格。
#creating variables to be used
text_in_use = ''
encrypt_key = ''
decrypt_key = ''
#function to encypypt input text
def encrypt(plaintext, key):
keyLength = len(key)
keyAsIntegers = [ord(i) for i in key] #create list with the ASCII value for each charachter in key
plaintextAsIntegers = [ord(i) for i in plaintext] #create list with the ASCII value for each charachter in text
encyptedtext = ''
for i in range(len(plaintextAsIntegers)): #
encryptvalue = (plaintextAsIntegers[i] + keyAsIntegers[i % keyLength]) % 26 #execute encryption or characters according to vigenere definition
encyptedtext += chr(encryptvalue + 65)
return encyptedtext #return the encyptes tex
#function to decrypt the encrypted text
def decrypt(encyptedtext, key):
keyLength = len(key)
keyAsIntegers = [ord(i) for i in key] #create list with the ASCII value for each charachter in key
encryptedTextAsIntegers = [ord(i) for i in encyptedtext] #create list with the ASCII value for each charachter in text
plaintext = ''
for i in range(len(encryptedTextAsIntegers)):
value = (encryptedTextAsIntegers[i] - keyAsIntegers[i % keyLength]) % 26 #decryption of encrypted characters
plaintext += chr(value + 65)
return plaintext #return decrypted text
#check if user input is valid
def check_value(userEntry):
while True:
try: #check if userinput is an integer
userInput = int(input(userEntry))
if userInput not in range(1,6): #check if userinput is in valid range
print("Invalid choice, valid choices are 1-5! Try again! \n")
except ValueError:
print("Invalid choice! Input can't be empty or a string! \n")
print("""1: Input text to work with
2: Print the current text
3: Encrypt the current text
4: Decrypt the current text
5: Exit""")
else:
return userInput #return valid userinput
def menu():
while True:
print("""1: Input text to work with
2: Print the current text
3: Encrypt the current text
4: Decrypt the current text
5: Exit""")
choice = check_value("Enter Choice: ")
if choice == 1: #allows user to input text for use
text_in_use = str(input("Enter Text: ")).upper()
print("Text is set to:",text_in_use,"\n")
elif choice == 2: #prints set text
print("Your text:",text_in_use,"\n")
elif choice == 3: #ask user to set encryptionkey
encrypt_key = str(input("Enter an encryptionkey: ")).upper()
text_in_use = encrypt(text_in_use, encrypt_key)
print("Your text:", text_in_use)
elif choice == 4: #ask user for decryptionkey
decrypt_key = str(input("Enter a the decryptionkey: ")).upper()
text_in_use = decrypt(text_in_use, decrypt_key)
print("Your text:", text_in_use)
elif choice == 5:
exit()
menu()
我希望程序像它已经做的那样工作,但它应该跳过加密中的空格。
如:
"HELLO MY MAN" --> encryption(key = asd) --> "HWOLG MQ MSQ"
换句话说,空格应该仍然存在于加密文本中。
【问题讨论】:
-
加密的全部目的是使密文看起来尽可能随机。如果你保留空间,它会更容易破解。无论如何,常规算法也会加密空格,因此您应该修改算法或单独加密每个单词
-
这有一些骗局,但是这个措辞真的很好,而其他的似乎更糟糕的是代码错误/附加问题/更具体的代码特定问题,并且通常没有得到回答,所以工藤为此。但是,请特别注意标题正确,并在发布前查看您的帖子。
标签: python python-3.x encryption whitespace vigenere