//Function called from the loop populate the main array chars->colors to determine the index to be considered next. It includes various arbitrary modifications increasing the security //of the method. private RGBInfo updateRGB(RGBInfo curRGB, List<Color> availableColors) { int curIndex = curStep; if (curRGB.lastR >= 100) curIndex = curIndex + 1; if (curRGB.lastG < 100) curIndex = curIndex + 2; if (curRGB.lastB >= 100) curIndex = curIndex - 2; if (curIndex + curRGB.lastIndex < availableColors.Count - 10) curIndex = curIndex + curRGB.lastIndex; if (curLength <= 10) curIndex = curIndex - 1; else curIndex = curIndex + 1; if (curIndex < 0) curIndex = 0; if (curIndex > availableColors.Count - 1) curIndex = availableColors.Count - 1; curRGB.curIndex = curIndex; curRGB.lastIndex = curRGB.curIndex; return curRGB; }
//Building the basic "hashing dictionary", charsVsColors, where each (recognised) character is related to a given color. The relationships characters->colors is different every time and varies //on account of different variables (e.g., encrypting integer or length of the input text) private void buildCharsVsColors() { charsVsColors = new Dictionary<char, Color>(); Color curColor = startColor; int minIndex = 0; int maxIndex = 240; //The restricted number of colors forces to choose just specific characters RGBInfo curRGB = new RGBInfo(); curRGB.lastR = startColor.R; curRGB.lastG = startColor.G; curRGB.lastB = startColor.B; curRGB.lastIndex = calculateFirst(); //Calculating the start index (from the original List of known colors) from which all the analysis will be started reorderKnownColors(curRGB.lastIndex); //Reordering the original list with all the known colors on account of the inputs List<Color> availableColors = new List<Color>(curColors); for (int i = minIndex; i < maxIndex; i++) { if (!(i >= 127 && i <= 160)) //The restricted number of colors forces to choose just specific characters { string noDiacritics = WrittenByOthers.RemoveDiacritics(((char)i).ToString(), true); if (!(noDiacritics != ((char)i).ToString())) //The restricted number of colors forces to choose just specific characters { curRGB = updateRGB(curRGB, availableColors); curColor = availableColors[curRGB.curIndex]; curRGB.lastR = curColor.R; curRGB.lastG = curColor.G; curRGB.lastB = curColor.B; charsVsColors.Add((char)i, curColor); availableColors.RemoveAt(curRGB.curIndex); //Avoiding the given color to be considered for a different character } } } if (availableColors.Count > 0 && charsVsColors.ContainsValue(startColor)) { Color replaceColor = availableColors[0]; //There has to be always unused availableColors KeyValuePair<char, Color> tempPair = charsVsColors.FirstOrDefault(x => IO.twoColorsAreEqual(x.Value, startColor)); replacePair = new KeyValuePair<char, Color>(tempPair.Key, replaceColor); availableColors.RemoveAt(0); } int afterIndex = curIO.allKnownColors.IndexOf(startColor) + encryptingNumber; if (afterIndex > curIO.allKnownColors.Count - 1) afterIndex = encryptingNumber; Color afterOffset = curIO.allKnownColors[afterIndex]; startOffset.afterOffset = afterOffset; if (availableColors.Count > 0 && charsVsColors.ContainsValue(startOffset.afterOffset)) { Color replaceColor = availableColors[0]; //There has to be always unused availableColors KeyValuePair<char, Color> tempPair = charsVsColors.FirstOrDefault(x => IO.twoColorsAreEqual(x.Value, startOffset.afterOffset)); this.startOffset.replaceAfter = new KeyValuePair<char, Color>(tempPair.Key, replaceColor); availableColors.RemoveAt(0); } }