Deserboy

Message Decrypter

Aug 4th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 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.  
  7. namespace ConsoleApp4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int count = int.Parse(Console.ReadLine());
  14.             for (int i = 1; i <= count; i++)
  15.             {
  16.                 string result = "";
  17.                 string message = Console.ReadLine();
  18.                 string[] tokens = message.Split(": ").ToArray();
  19.                 string tag = tokens[0];
  20.                 string numbers = tokens[1];
  21.                 Regex regex = new Regex(@"^(\$|%)[A-Z][a-z]+\1: \[\d+\]\|\[\d+\]\|\[\d+\]\|$");
  22.                 bool match = regex.IsMatch(message);
  23.                 if (match == false)
  24.                 {
  25.                     result = "Valid message not found!";
  26.                     Console.WriteLine(result);
  27.                 }
  28.                 else
  29.                 {
  30.                     Regex groups = new Regex(@"^(\$|%)[A-Z][a-z]+\1: \[(\d+)\]\|\[(\d+)\]\|\[(\d+)\]\|$");
  31.                     Match matching = groups.Match(message);
  32.                     string firstNumGroup = matching.Groups[2].Value;
  33.                     string secondNumGroup = matching.Groups[3].Value;
  34.                     string thirdNumGroup = matching.Groups[4].Value;
  35.                     int firstNumber = int.Parse(firstNumGroup);
  36.                     int secondNumber = int.Parse(secondNumGroup);
  37.                     int thirdNumber = int.Parse(thirdNumGroup);
  38.                     string oldChar1 = firstNumber.ToString();
  39.                     string oldChar2 = secondNumber.ToString();
  40.                     string oldChar3 = thirdNumber.ToString();
  41.                     char newChar = (char)firstNumber;
  42.                     string newChar1 = newChar.ToString();
  43.                     char new2Char = (char)secondNumber;
  44.                     string new2Char2 = new2Char.ToString();
  45.                     char new3Char = (char)thirdNumber;
  46.                     string new3Char3 = new3Char.ToString();
  47.                     numbers = numbers.Replace(oldChar1, newChar1);
  48.                     numbers = numbers.Replace(oldChar2, new2Char2);
  49.                     numbers = numbers.Replace(oldChar3, new3Char3);
  50.                     string decryptedMsg = newChar1 + new2Char2 + new3Char3;
  51.                     string newTag = tag.Remove(0, 1);
  52.                     newTag = newTag.Remove(newTag.Length - 1, 1);
  53.                     Console.WriteLine($"{newTag}: {decryptedMsg}");
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment