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.Threading.Tasks;
- namespace _04.DecoddeAndDecrypt
- {
- class Program
- {
- static StringBuilder build = new StringBuilder();
- static void Main(string[] args)
- {
- string cypheredMess = Console.ReadLine();
- string lengthCypherStr = FindLengthOfCypher(cypheredMess);
- int lengthCypher = int.Parse(lengthCypherStr);
- //Console.WriteLine("cypher length " + lengthCypher);
- string inputEncoded = Encode(cypheredMess, lengthCypherStr);
- //Console.WriteLine(inputEncoded.Length);
- string cypher = inputEncoded.Substring(inputEncoded.Length - lengthCypher);
- string code = inputEncoded.Remove(inputEncoded.Length - lengthCypher);
- int maxLength = 0;
- if (cypher.Length >= code.Length)
- {
- maxLength = cypher.Length;
- }
- else
- {
- maxLength = code.Length;
- }
- //Console.WriteLine("code " + code);
- //Console.WriteLine("cypher " + cypher);
- Console.WriteLine(Encrypt(code, cypher, maxLength));
- }
- static string FindLengthOfCypher(string cypheredMess)
- {
- build.Clear();
- int indexTemp = cypheredMess.Length - 1;
- int lengthCypher;
- while (char.IsDigit(cypheredMess[indexTemp]))
- {
- indexTemp--;
- }
- string length = cypheredMess.Substring(indexTemp + 1);
- lengthCypher = int.Parse(length);
- return length;
- }
- static string Encode(string cypheredMess, string lengthCypherStr)
- {
- build.Clear();
- for (int i = 0; i < cypheredMess.Length - lengthCypherStr.Length; i++)
- {
- char current = cypheredMess[i];
- int number;
- string numberTemp;
- if (int.TryParse(current.ToString(), out number))
- {
- int tempIndex = i;
- while (char.IsDigit(cypheredMess[i]))
- {
- i++;
- }
- if (i - tempIndex == 1)
- {
- numberTemp = cypheredMess[i - 1].ToString();
- }
- else
- {
- numberTemp = cypheredMess.Substring(tempIndex, i - tempIndex);
- }
- number = int.Parse(numberTemp);
- for (int j = 0; j < number; j++)
- {
- build.Append(cypheredMess[i]);
- }
- }
- else
- {
- build.Append(current);
- }
- }
- return build.ToString();
- }
- static int FindIndex(char charLetter)
- {
- int index = 0;
- index = (int)charLetter - 65;
- return index;
- }
- static string Encrypt(string code, string cypher, int maxLength)
- {
- build.Clear();
- int indexCode = 0;
- int indexCypher = 0;
- while (true)
- {
- if (indexCode == code.Length)
- {
- indexCode = 0;
- }
- if (indexCypher == cypher.Length)
- {
- indexCypher = 0;
- }
- int indexCharCode = FindIndex(code[indexCode]);
- int indexCharCypher = FindIndex(cypher[indexCypher]);
- int indexResult = indexCharCode ^ indexCharCypher;
- indexResult += 65;
- char result = (char)indexResult;
- build.Append(result.ToString());
- if (code.Length < cypher.Length && indexCode == code.Length - 1)
- {
- code = build.ToString();
- build.Clear();
- }
- indexCode++;
- indexCypher++;
- if (indexCode == maxLength || indexCypher == maxLength)
- {
- if (indexCypher == maxLength)
- {
- for (int i = indexCode; i < code.Length; i++)
- {
- build.Append(code[i].ToString());
- }
- }
- break;
- }
- }
- return build.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment