Categories
Visual C# Телерик

Решение на задача – Decode And Decrypt

using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;

namespace DecodeAndDecrypt {
  class Program {
    static void Main() {
      string encodedMessage = Console.ReadLine();
      List cypherDigitsList = new List();
      for (int letterIndex = encodedMessage.Length - 1; letterIndex >= 0; letterIndex--) {
        if (Char.IsDigit(encodedMessage[letterIndex])) {
          cypherDigitsList.Add(encodedMessage[letterIndex]);
        } else {
          break;
        }
      }
      cypherDigitsList.Reverse();
      int cypherLenght = int.Parse(string.Join(string.Empty, cypherDigitsList.ToArray()));
      string decodedMessage = Decode(encodedMessage, cypherLenght.ToString().Length);
      int cypherStartIndex = decodedMessage.Length - cypherLenght;
      string cypher = decodedMessage.Substring(cypherStartIndex, cypherLenght);
      int encryptedMessageLength = decodedMessage.Length - cypher.Length;
      string encryptedMessage = decodedMessage.Substring(0, encryptedMessageLength);
      Console.WriteLine(Decrypt(encryptedMessage, cypher));
    }

    static string Decrypt(string encryptedMessage, string cypher) {
      char[] cypherText = new char[encryptedMessage.Length];
      int index = 0;
      if (cypher.Length < encryptedMessage.Length) {
        for (int i = 0; i < encryptedMessage.Length; i++) {
          if (index == cypher.Length) {
            index = 0;
          }
          cypherText[i] = (char)((FindLetterIndex((encryptedMessage[i])) ^ FindLetterIndex(cypher[index])) + 65);
          index++;
        }
      } else {
        for (int i = 0; i < cypher.Length; i++) {
          if (index == encryptedMessage.Length) {
            index = 0;
          }
          if (i >= encryptedMessage.Length) {
            cypherText[index] = (char)((FindLetterIndex((cypherText[index])) ^ FindLetterIndex(cypher[i])) + 65);
          } else {
            cypherText[index] = (char)((FindLetterIndex((encryptedMessage[index])) ^ FindLetterIndex(cypher[i])) + 65);
          }
          index++;
        }
      }
      string encryptedText = string.Join(string.Empty, cypherText);
      return encryptedText;
    }

    static string Decode(string message, int cypherDigitsCount) {
      StringBuilder decodedMessage = new StringBuilder();
      StringBuilder letterMultiplier = new StringBuilder();
      for (int i = 0; i < message.Length - cypherDigitsCount; i++) {
        if (Char.IsDigit(message[i])) {
          letterMultiplier.Append(Char.GetNumericValue(message[i]));
        } else {
          if (letterMultiplier.Length > 0) {
            decodedMessage.Append(new string(message[i], int.Parse(letterMultiplier.ToString())));
            letterMultiplier.Clear();
          } else {
            decodedMessage.Append(message[i]);
          }
        }
      }
      return decodedMessage.ToString();
    }

    static int FindLetterIndex(char letter) {
      int index = (letter < 97 ? letter - 65 : letter - 97);
      return index;
    }
  }
}