Hello. There is a code of Caesar Cipher with a key, tell me how to organize the issuance of a complete search, i.e. so that there is no key and shows each shift option.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace cezar { class Program { static void Main(string[] args) { int n = 1, key = 1; Console.WriteLine("Введите слово,которое нужно зашифровать:"); string s = Console.ReadLine(); Console.WriteLine("Введите ключ:"); key = Convert.ToInt32(Console.ReadLine()); string s1 = ""; string alfphabet = "АБВГДЕЖЗИКЛМНОПРСТУФХЦЧШЩЬЪЭЮЯ"; int m = alfphabet.Length; for (int i = 0; i < s.Length; i++) { for (int j = 0; j < alfphabet.Length; j++) { if (s[i] == alfphabet[j]) { int temp = j * n + key; while (temp >= m) temp -= m; s1 = s1 + alfphabet[temp]; } } } Console.WriteLine("Зашифрованное слово:" + s1); Console.ReadLine(); } } } 
  • Each variation of the shift, is it the length of a word? - Yury Bakharev
  • There is an alphabet, in this case "lfphabet =" ABCGDEZHIZKLMNOPRSTUFHCSCHSCHSCHYEYUYA ";", you need to display all permutation options, in this case 31 - KksMM
  • And why do you need j * n? - Yury Bakharev
  • This is the letter number I need, instead of "key" was a set of numbers from 1 to the length of the alphabet - KksMM

1 answer 1

Well, if I understand you, you can try this

  static void Main() { Console.WriteLine("Введите слово,которое нужно зашифровать:"); string s = Console.ReadLine(); string alfphabet = "АБВГДЕЖЗИКЛМНОПРСТУФХЦЧШЩЬЪЭЮЯ"; int m = alfphabet.Length; List<string> result = new List<string>(); for(int y=1; y<32; y++) { result.Add(Shift(s, alfphabet, y)); } foreach (var word in result) { Console.WriteLine("Зашифрованное слово:" + word); } Console.ReadLine(); } static string Shift(string targetWord, string alfphabet , int key) { string result = string.Empty; for (int i = 0; i < targetWord.Length; i++) { for (int j = 0; j < alfphabet.Length; j++) { if (targetWord[i] == alfphabet[j]) { int temp = j + key; while (temp >= alfphabet.Length) temp -= alfphabet.Length; result += alfphabet[temp]; } } } return result; } 
  • I swear at "result. Add (Shift (s, alfphabet, y));" , for a non-static field, a reference to an object is required. ((At the same time, advise with what literature to start studying Sharp?))) - KksMM
  • put static at the beginning of the method (I corrected in the answer). about literature ru.stackoverflow.com/questions/416584/… - Yury Bakharev
  • You saved me, now I will definitely start studying, I am ashamed of that. - KksMM
  • If everything works, you can accept the answer and mark it as correct - Yury Bakharev