Guest User

Untitled

a guest
Sep 14th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. string qwerty = "qwertyuiopasdfghjklzxcvbnm";
  10. qwerty += qwerty.ToUpper();
  11. char[] alphabet = qwerty.ToCharArray();
  12. Array.Sort(alphabet);
  13.  
  14. Console.WriteLine("1 - зашифровать строку\n2 - дешифровать строку\n0 - выйти из программы");
  15. string choice = Console.ReadLine();
  16. while (choice != "0")
  17. {
  18. switch (choice)
  19. {
  20. case "1": Encrypt(alphabet); break;
  21. case "2": Decrypt(alphabet); break;
  22. default: Console.WriteLine("Неизвестная команда"); break;
  23. }
  24. Console.WriteLine("Что вы хотите сделать?");
  25. choice = Console.ReadLine();
  26. }
  27. }
  28. static void Encrypt(char[] alphabet)
  29. {
  30. Console.WriteLine("Введите слово:");
  31. string word = Console.ReadLine();
  32. Console.WriteLine("Введите ключ:");
  33. string key = Console.ReadLine();
  34.  
  35. string result = "";
  36. string keyWord = "";
  37. int counter = 0;
  38.  
  39. foreach (char letter in word)
  40. {
  41. if (letter != ' ')
  42. {
  43. if (counter == key.Length)
  44. counter = 0;
  45. keyWord += key[counter];
  46. counter++;
  47. }
  48. else keyWord += ' ';
  49. }
  50.  
  51. for (int letter = 0; letter < word.Length; letter++)
  52. {
  53. if (word[letter] != ' ')
  54. {
  55. int wordIndex = Array.IndexOf(alphabet, word[letter]);
  56. int keyIndex = Array.IndexOf(alphabet, keyWord[letter]);
  57. int encIndex = (wordIndex + keyIndex) % alphabet.Length;
  58. result += alphabet[encIndex];
  59. }
  60. else result += ' ';
  61. }
  62.  
  63. Console.WriteLine(result);
  64. }
  65. static void Decrypt(char[] alphabet)
  66. {
  67. Console.WriteLine("Введите слово:");
  68. string word = Console.ReadLine();
  69. Console.WriteLine("Введите ключ:");
  70. string key = Console.ReadLine();
  71.  
  72. string result = "";
  73. string keyWord = "";
  74. int counter = 0;
  75.  
  76. foreach (char letter in word)
  77. {
  78. if (letter != ' ')
  79. {
  80. if (counter == key.Length)
  81. counter = 0;
  82. keyWord += key[counter];
  83. counter++;
  84. }
  85. else keyWord += ' ';
  86. }
  87.  
  88. for (int letter = 0; letter < word.Length; letter++)
  89. {
  90. if (word[letter] != ' ')
  91. {
  92. int wordIndex = Array.IndexOf(alphabet, word[letter]);
  93. int keyIndex = Array.IndexOf(alphabet, keyWord[letter]);
  94. int encIndex = (wordIndex - keyIndex + alphabet.Length) % alphabet.Length;
  95. result += alphabet[encIndex];
  96. }
  97. else result += ' ';
  98. }
  99.  
  100. Console.WriteLine(result);
  101. }
  102. }
  103. }
Add Comment
Please, Sign In to add comment