public IEnumerable <char> Decode(string stringToDecode) { if (stringToDecode.Any(n => !Alphabet.Contains(n) && !Separators.Contains(n))) { throw new Exception("Finded symbol not from alphabet"); } string s = string.Empty; for (int i = 0; i < stringToDecode.Length; i++) { if (Separators.Any(n => n == stringToDecode[i])) { s += stringToDecode[i]; } else { s += Alphabet[LookupTable[i % KeyWord.Length].IndexOf(stringToDecode[i])]; } } return(s); }
//Encrypts the message using the key public static string Encrypt(string message, string key) { message = message.ToUpper(); key = key.ToUpper(); string plaintext = ""; int i = 0; foreach (char element in message) { if (!Char.IsLetter(element)) { plaintext += ""; } else { int MOrder = Alphabet.EnglishAlphabet().FirstOrDefault(x => x.Value == element).Key; int KOrder = Alphabet.EnglishAlphabet().FirstOrDefault(x => x.Value == key[i]).Key; int Final = (int)(MOrder - KOrder); if (Final < 0) { Final += 26; } plaintext += Alphabet.EnglishAlphabet()[Final]; i++; } if (i == key.Length) { i = 0; } } return(plaintext); }