I have made a menu based encryption tool using Vigenére cipher. As of now the program encrypts white spaces, how can I get the program to skip over white spaces.
#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()
I want the program to work as it already does but it should skip whitespaces in the encryption.
Such as:
"HELLO MY MAN" --> encryption(key = asd) --> "HWOLG MQ MSQ"
In other words, the white spaces should still be there in the encrypted text.