"Encrypting" just spits out original message? why!!

This is based off of OTP, and is for a project of mine. I seam to be missing something and I don’t know what I am missing, or what is going wrong!

The encrypting is more of a cypher, and it is usually done on pen an paper, and it is meant to work like this:
you have a table of letters, a message, and a key. The table of letters are offset by one for every time you go down. You find each letter of the message in the first row, and meet with each letter of the key in the first column, where each pair meets is the encrypted message. The problem is, when I try to do that in unity it just spits out the unencrypted message!

the odd thing is that when I do this by hand, with the same information the debug gives me, I get a fully encrypted message! its like the nested list for my letter table is just being ignored and it is instead just putting the message there.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class Cryptic : MonoBehaviour
{
    public List<string> CrypticLeters;

    public List<char> keyList;
    public List<char> messageList;
    //to encrypt. Find the leter of message in the first line, cryptAlphabet[0], also find the key in the same line
    //go to cryptAlphabet[key][message]. that is your encryption

    //to decrypt find the location of key in cryptAlphabet[0], find the location of encrypted message in cryptAlphabet[key]
    //find what is in cryptAlphabet[0] at the location of the encrypted message in cryptAlphabet[key]

    // Start is called before the first frame update
    void Start()
    {

        CrypticLeters = new List<string>
            {
                "~",
                "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                ",",".","!","?","'","_","@","#","$","%","&","*","(",")","-","=","+","/","<",">",
                "1","2","3","4","5","6","7","8","9","0",
                " "
            };

        string encryptedMessage = Encrypting(
            CrypticLeters,
              "Tacos are good! <3",
              "8hDEEBHD1O nk2Sew5Poi vSHdFaZE3u de1mc9Ea8y"
              );

        Debug.Log(encryptedMessage);
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public string Encrypting(List<string> CryptLetterset, string Message, string Key )
    {
        string MessageEncrypt = "";

        //make key and message list
         keyList = Key.ToCharArray().ToList();
         messageList = Message.ToCharArray().ToList();

        //get letter set, or alphabet, for encrypting
        List<List<string>> cryptAlphabet = lettersetArray(CryptLetterset);

    
        int cryptLoop = 0;
        int keyLoop = 0;

        while (cryptLoop < messageList.Count)
        {
            //if ran out of key, restart key
            if (keyLoop >= keyList.Count) { keyLoop = 0; }

            //get where the key and message letters are in the letterset
            int keyInex = cryptAlphabet[0].IndexOf(keyList[keyLoop].ToString());
            int messageIndex = cryptAlphabet[0].IndexOf(messageList[cryptLoop].ToString());

            //add encrypted letter to message
            MessageEncrypt += cryptAlphabet[keyInex][messageIndex];
            Debug.Log("keyInex |" + keyInex.ToString() + "  messageIndex |" + messageIndex.ToString() + "  Message |" + MessageEncrypt);
           keyLoop++;
            cryptLoop++;
        }


         
        return (MessageEncrypt);
    }

    //make an array of arrays of letter, getting that letter set table required for
    //otp encryption
    List<List<string>> lettersetArray(List<string> CryptLetterset)
    {

        //make the alphabets
        List<List<string>> cryptAlphabet = new List<List<string>>();
       
        //put in the first row in the table
        cryptAlphabet.Add(CryptLetterset);
        string Somethingg = string.Join(",", cryptAlphabet[0]);
       
        //debug
        int masterLoop = 1;
        Debug.Log(masterLoop.ToString() + " - " + Somethingg);

        //shift the alphabet by one and adding it to the table
        while (masterLoop < CryptLetterset.Count)
        {
            cryptAlphabet.Add(CryptLetterset);
            cryptAlphabet[masterLoop] = cryptAlphabet[(masterLoop - 1)];
            cryptAlphabet[masterLoop].Add(cryptAlphabet[masterLoop - 1][0]);
            cryptAlphabet[masterLoop].RemoveAt(0);

            string Something = string.Join(",", cryptAlphabet[masterLoop]);
            Debug.Log(masterLoop.ToString() + " - " + Something);
            masterLoop++;
        }

        return (cryptAlphabet);

    }


}

There’s nothing to do but debug. (and yes it’s caused by a bug in your code).

Print out everything until you find something that is not in the right place. Then focus your attentions there.

There is some discrepancy between what your code is doing and what you are doing by hand.

A big part of this debugging process should be breaking up lines of code like this:
int keyInex = cryptAlphabet[0].IndexOf(keyList[keyLoop].ToString());
Break out each smaller bit of that line into its own separate line and analyze each piece. You may find something out of place.

1 Like

Attach the debugger, run the the program, and step through it line by line and see what is happening.

if anyone is wondering, no, this problem is not fixed. I asked for help with a bug in my code and all I have been told was “have you tried debugging?”

are you saying you found a bug in my code?

The bug is that you are not using the ready togo classes in System.Security.Cryptography

1 Like

I did not find the bug, but I know the bug is there, because of the behavior you have described :smile:

2 Likes

ah! ty for that information

I am not using System.Security.Cryptography, the project is to create my own Cryptography. I have this all done except for the odd function that is the nested list not returning what it should

update, I am rewriting this using List where list<list> is, that seams to be the issue. The list of string turns out to just be one letter, which is annoying and makes me hate everything, but ok, will rewrite