geniusvil

DecoddeAndDecrypt

Jan 19th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04.DecoddeAndDecrypt
  8. {
  9. class Program
  10. {
  11. static StringBuilder build = new StringBuilder();
  12.  
  13. static void Main(string[] args)
  14. {
  15. string cypheredMess = Console.ReadLine();
  16.  
  17. string lengthCypherStr = FindLengthOfCypher(cypheredMess);
  18. int lengthCypher = int.Parse(lengthCypherStr);
  19. //Console.WriteLine("cypher length " + lengthCypher);
  20.  
  21.  
  22. string inputEncoded = Encode(cypheredMess, lengthCypherStr);
  23. //Console.WriteLine(inputEncoded.Length);
  24. string cypher = inputEncoded.Substring(inputEncoded.Length - lengthCypher);
  25. string code = inputEncoded.Remove(inputEncoded.Length - lengthCypher);
  26. int maxLength = 0;
  27. if (cypher.Length >= code.Length)
  28. {
  29. maxLength = cypher.Length;
  30. }
  31. else
  32. {
  33. maxLength = code.Length;
  34. }
  35. //Console.WriteLine("code " + code);
  36. //Console.WriteLine("cypher " + cypher);
  37. Console.WriteLine(Encrypt(code, cypher, maxLength));
  38. }
  39.  
  40. static string FindLengthOfCypher(string cypheredMess)
  41. {
  42. build.Clear();
  43. int indexTemp = cypheredMess.Length - 1;
  44. int lengthCypher;
  45. while (char.IsDigit(cypheredMess[indexTemp]))
  46. {
  47. indexTemp--;
  48. }
  49. string length = cypheredMess.Substring(indexTemp + 1);
  50. lengthCypher = int.Parse(length);
  51. return length;
  52. }
  53.  
  54. static string Encode(string cypheredMess, string lengthCypherStr)
  55. {
  56. build.Clear();
  57. for (int i = 0; i < cypheredMess.Length - lengthCypherStr.Length; i++)
  58. {
  59. char current = cypheredMess[i];
  60. int number;
  61. string numberTemp;
  62. if (int.TryParse(current.ToString(), out number))
  63. {
  64. int tempIndex = i;
  65. while (char.IsDigit(cypheredMess[i]))
  66. {
  67. i++;
  68. }
  69. if (i - tempIndex == 1)
  70. {
  71. numberTemp = cypheredMess[i - 1].ToString();
  72. }
  73. else
  74. {
  75. numberTemp = cypheredMess.Substring(tempIndex, i - tempIndex);
  76. }
  77. number = int.Parse(numberTemp);
  78.  
  79. for (int j = 0; j < number; j++)
  80. {
  81. build.Append(cypheredMess[i]);
  82. }
  83. }
  84. else
  85. {
  86. build.Append(current);
  87. }
  88. }
  89.  
  90. return build.ToString();
  91. }
  92.  
  93. static int FindIndex(char charLetter)
  94. {
  95. int index = 0;
  96. index = (int)charLetter - 65;
  97. return index;
  98. }
  99.  
  100. static string Encrypt(string code, string cypher, int maxLength)
  101. {
  102. build.Clear();
  103. int indexCode = 0;
  104. int indexCypher = 0;
  105.  
  106. while (true)
  107. {
  108. if (indexCode == code.Length)
  109. {
  110. indexCode = 0;
  111. }
  112. if (indexCypher == cypher.Length)
  113. {
  114. indexCypher = 0;
  115. }
  116.  
  117. int indexCharCode = FindIndex(code[indexCode]);
  118.  
  119. int indexCharCypher = FindIndex(cypher[indexCypher]);
  120.  
  121. int indexResult = indexCharCode ^ indexCharCypher;
  122. indexResult += 65;
  123.  
  124. char result = (char)indexResult;
  125.  
  126. build.Append(result.ToString());
  127.  
  128. if (code.Length < cypher.Length && indexCode == code.Length - 1)
  129. {
  130. code = build.ToString();
  131. build.Clear();
  132. }
  133.  
  134. indexCode++;
  135. indexCypher++;
  136.  
  137. if (indexCode == maxLength || indexCypher == maxLength)
  138. {
  139. if (indexCypher == maxLength)
  140. {
  141. for (int i = indexCode; i < code.Length; i++)
  142. {
  143. build.Append(code[i].ToString());
  144. }
  145. }
  146. break;
  147. }
  148. }
  149.  
  150. return build.ToString();
  151. }
  152.  
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment