Advertisement
mustafov

Decrypt the Messages

Sep 3rd, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.39 KB | None | 0 0
  1. // Decrypt the Messages
  2. //You are working for a company which is very concerned about its information and communication. For this reason, they have //invented an internal approach to communication between different departments – they are communicating to each other via messages, //which are reversed (written backwards) and then encrypted. In order to be read and understood, each message has to be decrypted. //Your task is to write a program, which receives all encrypted messages in a specific communication, decrypts them and prints all //decrypted messages at the console as well as the total number of messages that have been received.
  3. //At the beginning of a communication, you will receive either the keyword “START” (upper case) or “start” (lower case), which //indicates that you will start receiving reversed and encrypted messages. At the end of the communication, you will receive either //the keyword “END” (upper case) or “end” (lower case), which indicates that the communication is over and you need to show the //decrypted messages’ content and total count. Any non-empty string between the “start” and “end” keywords is considered a message. //If no messages have been received between the “start” and the “end” keywords, you should print on the console: “No message //received.”
  4. //All messages are case-sensitive and consist of letters, digits, as well as some special characters – ‘+’, ‘%’, ‘&’, ‘#’ and ‘$’. //Letters from A to M are converted to letters from N to Z (A  N; B  O; … M  Z) and letters from N to Z are converted to letters //from A to M (N  A; O  B; … Z  M). The converted letter should keep the case of the original letter. The special characters are //converted in the following way: ‘+’ is converted to a single space (‘  ’), ‘%’ is converted to a comma (‘,’), ‘&’ is converted to //a dot (‘.’), ‘#’ is converted to a question mark (‘?’) and ‘$’ is converted to an exclamation mark (‘!’). The digits (0-9) are //not converted and stay the same.
  5. //For example, you receive the following message – “$1+rtnffrz+greprF” and you start decrypting it. Convert the 1st character ‘$’ //to ‘!’, then the 2nd character – ‘1’ stays the same, then covert the 3rd character – ‘+’ to single space ‘ ’, ‘r’  ’e’, ‘t’  //‘g’, ‘n’  ‘a’, ‘f’  ‘s’, ‘f’  ‘s’, ‘r’  ’e’ , ‘z’  ’m’, ‘+’  ‘ ’, ‘g’  ‘t’, ‘r’  ’e’ , ‘e’  ’r’ , ‘p’  ’c’ , ‘r’  ’e’ //, ‘F’  ’S’. After decrypting all letters, the message is: “!1 egassem terceS” and when you reverse it, you get the original //message: “Secret message 1!”
  6.  
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13.  
  14. namespace ConsoleApplication2
  15. {
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.  
  21.             string input;
  22.             StringBuilder newInput = new StringBuilder();
  23.             int p = 0;
  24.  
  25.             List<string> arr = new List<string>();
  26.  
  27.             int messages = 0;
  28.             do
  29.             {
  30.                 input = Console.ReadLine();
  31.                 if (input == "START" || input == "start")
  32.                 {
  33.                     int lenght = input.Length;
  34.                     p = 0;
  35.                     newInput = new StringBuilder();
  36.  
  37.                     if (input != "end" && input != "END" && input != "start" && input != "START")
  38.                     {
  39.                         for (int i = 0; i < lenght; i++)
  40.                         {
  41.                             if (input[i] >= 'a' && input[i] <= 'm')
  42.                             {
  43.                                 newInput.Insert(p, (char)(input[i] + 13));
  44.                                 p++;
  45.                             }
  46.                             else if (input[i] >= 'n' && input[i] <= 'z')
  47.                             {
  48.                                 newInput.Insert(p, (char)(input[i] - 13));
  49.                                 p++;
  50.                             }
  51.                             else if (input[i] >= 'A' && input[i] <= 'M')
  52.                             {
  53.                                 newInput.Insert(p, (char)(input[i] + 13));
  54.                                 p++;
  55.                             }
  56.                             else if (input[i] >= 'N' && input[i] <= 'Z')
  57.                             {
  58.                                 newInput.Insert(p, (char)(input[i] - 13));
  59.                                 p++;
  60.                             }
  61.                             else if (input[i] == '+')
  62.                             {
  63.                                 newInput.Insert(p, (char)(input[i] - 11));
  64.                                 p++;
  65.                             }
  66.                             else if (input[i] == '%')
  67.                             {
  68.                                 newInput.Insert(p, (char)(input[i] + 7));
  69.                                 p++;
  70.                             }
  71.                             else if (input[i] == '#')
  72.                             {
  73.                                 newInput.Insert(p, (char)(input[i] + 28));
  74.                                 p++;
  75.                             }
  76.                             else if (input[i] == '$')
  77.                             {
  78.                                 newInput.Insert(p, (char)(input[i] - 3));
  79.                                 p++;
  80.                             }
  81.                             else if (input[i] == '&')
  82.                             {
  83.                                 newInput.Insert(p, (char)(input[i] + 8));
  84.                                 p++;
  85.                             }
  86.                             else if (input[i] >= '0' && input[i] <= '9')
  87.                             {
  88.                                 newInput.Insert(p, (char)(input[i]));
  89.                                 p++;
  90.                             }
  91.  
  92.                         }
  93.                         string text = newInput.ToString();
  94.                         while ((input != "end" && input != "END") && lenght != 0)
  95.                         {
  96.                             arr.Add(text);
  97.                             messages++;
  98.                             break;
  99.                         }
  100.                     }
  101.                     else
  102.                     {
  103.                         break;
  104.                     }
  105.                 }
  106.             } while (input != "end" || input != "END");
  107.             if (messages == 0)
  108.             {
  109.                 Console.WriteLine("No message received.");
  110.             }
  111.             else
  112.             {
  113.                 Console.WriteLine("Total number of messages: {0}", messages);
  114.             }
  115.  
  116.             for (int k = 0; k < arr.Count; k++)
  117.             {
  118.                 string newInputt = arr[k].ToString();
  119.                 newInputt = Reverse(newInputt);
  120.                 Console.WriteLine(newInputt);
  121.             }
  122.         }
  123.         public static string Reverse(string str)
  124.         {
  125.             char[] chars = str.ToCharArray();
  126.             for (int i = 0, j = str.Length - 1; i < j; i++, j--)
  127.             {
  128.                 char c = chars[i];
  129.                 chars[i] = chars[j];
  130.                 chars[j] = c;
  131.             }
  132.             return new string(chars);
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement