Skip to main content

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

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data