Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace _02.Spy_Gram
- {
- class Program
- {
- static void Main(string[] args)
- {
- Dictionary<string, List<string>> allPendingMassageInformation = new Dictionary<string, List<string>>();
- string pattern = @"TO: (?<recipient>[A-Z]+); MESSAGE: (?<message>.+?);"; // Regex
- string input = string.Empty;
- string privateKey = Console.ReadLine();
- while ((input = Console.ReadLine()) != "END")
- {
- if (Regex.IsMatch(input, pattern)) // Проверка дали съвпада // ако не се пропуска
- {
- Match informationTokens = Regex.Match(input, pattern); // съвпадението го взимаме
- // вадим recipient и massage :
- string recipient = informationTokens.Groups["recipient"].ToString();
- string massage = informationTokens.Groups["message"].ToString();
- // вкарваме в речник ако нямаме срещане на recipient
- if (!allPendingMassageInformation.ContainsKey(recipient))
- {
- allPendingMassageInformation[recipient] = new List<string>();
- }
- // ако има срещан recipient ... то нека се добави съобщениято му
- if (allPendingMassageInformation.ContainsKey(recipient))
- {
- allPendingMassageInformation[recipient].Add(massage);
- }
- }
- }
- // подреждаме речника
- allPendingMassageInformation = allPendingMassageInformation
- .OrderBy(x => x.Key)
- .ToDictionary(x => x.Key, x => x.Value);
- // речник .. който ще държи всички recipient с цялото криптирано съобщение
- Dictionary<string, List<string>> allPendingEncriptedMassageInformation = new Dictionary<string, List<string>>();
- // от миналия речник ще взимаме
- foreach (var item in allPendingMassageInformation)
- {
- string recipient = item.Key;
- List<string> massagesOfRecipient = item.Value;
- List<string> EncriptedMassagesList = new List<string>();
- foreach (var massage in massagesOfRecipient)
- {
- string encription = Encrypt(massage, privateKey, recipient);
- EncriptedMassagesList.Add(encription);
- }
- allPendingEncriptedMassageInformation[recipient] = new List<string>(EncriptedMassagesList);
- }
- // Output //
- foreach (var item in allPendingEncriptedMassageInformation)
- {
- List<string> massages = item.Value;
- foreach (var massage in massages)
- {
- Console.WriteLine(massage);
- }
- }
- // Note : реших така да направя задачата ... за да може ако нещо объркам или не съм разбрал да извърша бързи корекции
- // вярвам че може да се реши по кратко + това позволява да използването на повече синтаксис
- //
- }
- // метода на криптиране на цялото съобщение
- private static string Encrypt(string massage, string privateKeyList, string recipient)
- {
- StringBuilder newMassage = new StringBuilder();
- string fullMassage = $"TO: {recipient}; MESSAGE: {massage};";
- for (int i = 0; i < fullMassage.Length; i++)
- {
- newMassage.Append((char)((int)fullMassage[i] + int.Parse((privateKeyList[i % privateKeyList.Length].ToString()))));
- }
- return newMassage.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement