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);
}
}