Advertisement
Guest User

Untitled

a guest
Dec 26th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _02.Spy_Gram
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Dictionary<string, List<string>> allPendingMassageInformation = new Dictionary<string, List<string>>();
  15.  
  16. string pattern = @"TO: (?<recipient>[A-Z]+); MESSAGE: (?<message>.+?);"; // Regex
  17. string input = string.Empty;
  18.  
  19. string privateKey = Console.ReadLine();
  20.  
  21. while ((input = Console.ReadLine()) != "END")
  22. {
  23. if (Regex.IsMatch(input, pattern)) // Проверка дали съвпада // ако не се пропуска
  24. {
  25. Match informationTokens = Regex.Match(input, pattern); // съвпадението го взимаме
  26.  
  27. // вадим recipient и massage :
  28. string recipient = informationTokens.Groups["recipient"].ToString();
  29. string massage = informationTokens.Groups["message"].ToString();
  30.  
  31. // вкарваме в речник ако нямаме срещане на recipient
  32. if (!allPendingMassageInformation.ContainsKey(recipient))
  33. {
  34. allPendingMassageInformation[recipient] = new List<string>();
  35. }
  36.  
  37. // ако има срещан recipient ... то нека се добави съобщениято му
  38. if (allPendingMassageInformation.ContainsKey(recipient))
  39. {
  40. allPendingMassageInformation[recipient].Add(massage);
  41. }
  42.  
  43. }
  44. }
  45.  
  46. // подреждаме речника
  47. allPendingMassageInformation = allPendingMassageInformation
  48. .OrderBy(x => x.Key)
  49. .ToDictionary(x => x.Key, x => x.Value);
  50.  
  51. // речник .. който ще държи всички recipient с цялото криптирано съобщение
  52. Dictionary<string, List<string>> allPendingEncriptedMassageInformation = new Dictionary<string, List<string>>();
  53.  
  54. // от миналия речник ще взимаме
  55. foreach (var item in allPendingMassageInformation)
  56. {
  57. string recipient = item.Key;
  58. List<string> massagesOfRecipient = item.Value;
  59.  
  60. List<string> EncriptedMassagesList = new List<string>();
  61.  
  62. foreach (var massage in massagesOfRecipient)
  63. {
  64. string encription = Encrypt(massage, privateKey, recipient);
  65. EncriptedMassagesList.Add(encription);
  66. }
  67.  
  68. allPendingEncriptedMassageInformation[recipient] = new List<string>(EncriptedMassagesList);
  69. }
  70.  
  71. // Output //
  72. foreach (var item in allPendingEncriptedMassageInformation)
  73. {
  74. List<string> massages = item.Value;
  75.  
  76. foreach (var massage in massages)
  77. {
  78. Console.WriteLine(massage);
  79. }
  80. }
  81.  
  82. // Note : реших така да направя задачата ... за да може ако нещо объркам или не съм разбрал да извърша бързи корекции
  83. // вярвам че може да се реши по кратко + това позволява да използването на повече синтаксис
  84. //
  85. }
  86.  
  87.  
  88. // метода на криптиране на цялото съобщение
  89. private static string Encrypt(string massage, string privateKeyList, string recipient)
  90. {
  91. StringBuilder newMassage = new StringBuilder();
  92. string fullMassage = $"TO: {recipient}; MESSAGE: {massage};";
  93.  
  94. for (int i = 0; i < fullMassage.Length; i++)
  95. {
  96. newMassage.Append((char)((int)fullMassage[i] + int.Parse((privateKeyList[i % privateKeyList.Length].ToString()))));
  97. }
  98.  
  99. return newMassage.ToString();
  100. }
  101.  
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement