How could I make my decryption program recognize which character the encrypted character belongs to, if each character could have 3 possible answers?
Heyo, I've had an assignment where I need to write an encryption program that gives each character three possible characters that it would turn into when encrypted. For example: a = [#, S, 2], b = [&, =, @]
import string
import random
import json
question = input("Would you like to Encrypt or Decrypt a string?\n")
if question == "Encrypt":
# We choose our random characters from encryptionList, and also create the dictionary from the same string using dict.fromkeys().
encryptionList = string.ascii_letters + string.digits + string.punctuation + " "
dictionary1 = dict.fromkeys(encryptionList, )
dictionary2 = dict.fromkeys(encryptionList, )
# Adding the three random characters to our Dictionary by random, while loop checks whether each index is similar, and chooses another one if it is.
for key in dictionary1:
listOfLetters1 = list()
listOfLetters2 = list()
for i in range(3):
listOfLetters1.append(random.choice(encryptionList))
for i in range(3):
listOfLetters2.append(random.choice(encryptionList))
for j in range(3):
while (listOfLetters1[j] == listOfLetters2[j]):
listOfLetters1[j] = random.choice(encryptionList)
dictionary1[key] = listOfLetters1
dictionary2[key] = listOfLetters2
# Asking for the string.
toEncrypt = input("Please enter a string to ecrypt:\n")
methodEncrypt = input("Which encryption method would you like? (1, 2)\n")
# Creating the encrypted message.
randomLetter = list()
if methodEncrypt == "1":
encryptedString = ""
for letter in toEncrypt:
dictLetter = dictionary1[letter]
randomLetter.append(random.choice(dictLetter))
key1 = open("Key1.txt", "w")
key1.write(json.dumps(dictionary1))
if methodEncrypt == "2":
encryptedString = ""
for letter in toEncrypt:
dictLetter = dictionary2[letter]
randomLetter.append(random.choice(dictLetter))
key2 = open("Key2.txt", "w")
key2.write(json.dumps(dictionary2))
# Using str(e) for e in randomLetter to swap everything inside the list into a string. ''.join() will join the elements of the list together.
encryptedMessage = ''.join(str(e) for e in randomLetter)
if methodEncrypt == "1":
print(f"Your encrypted message is now: {encryptedMessage}\nYour key is 'Key1'\nTo decrypt your message you will need this information.")
elif methodEncrypt == "2":
print(f"Your encrypted message is now: {encryptedMessage}\nYour key is 'Key2'\nTo decrypt your message you will need this information.")
elif question == "Decrypt":
# Asking for the string.
toDecrypt = input("Please enter a string to decrypt:\n")
methodDecrypt = input("Which method did you use to encrypt the string with? (1, 2)\n")
# Decrypting the message.
keyFile = input("Please enter the name of your Key:\n")
decryptKey = open(keyFile, "r")
decryptFile = decryptKey.read()
print(decryptFile)
decryptDict = json.loads(decryptFile)
decryptedString = ""
for letter in toDecrypt:
for char in decryptDict:
charList = decryptDict[char]
if letter in charList:
decryptedString += char
print(f"Your decrypted message is now: {decryptedString}\n")
My problem arises in the decryption portion of the code. Since the characters inside the lists could be repeated, for example the character "&" could be in two or more characters: a = [&, T, 4] and b = [%, &, 2] and c = [G, 1, &] How could I make it so the program can detect which one of these is the actual character we need, if ab turns into &2, how could I code it so it'd understand that the & is for a and not b and c, and not return cbb instead of ab as a result?
I was thinking of putting a signal code at the end of each character list, such as 1, 2, 3, 4, etc... and if that was the encrypted character it would add the signal code to the encrypted string as well (a = [&, T, 4, 1], if a were to be encrypted it would add 1 to the end of the encrypted string, and it would be later decoded from that signal code, but since numbers and all other characters could also be inside of the string that the user would like to encrypt, it would not work.
source https://stackoverflow.com/questions/76879005/how-could-i-make-my-decryption-program-recognize-which-character-the-encrypted-c
Comments
Post a Comment