Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5.  
  6. namespace SzyfrVigenera
  7. {
  8.  
  9. class Program
  10. {
  11.  
  12. static void Main(string[] args)
  13. {
  14.  
  15. inputPath = @"D:\Programowanie\SzyfrVigenera\SzyfrVigenera\tekst_Vigenere.txt";
  16. outputPath = @"D:\Programowanie\SzyfrVigenera\SzyfrVigenera\output.txt";
  17.  
  18. Console.WriteLine("Set encryption key:");
  19. key = Console.ReadLine().Trim();
  20.  
  21. string text = ReadTextFromFile(inputPath);
  22. string encryptedText = VigenereAlgorithm(text, key);
  23. WriteTextToFile(encryptedText, outputPath);
  24. Console.WriteLine();
  25.  
  26. Console.WriteLine("Set division into columns: ");
  27. divisionIntoColumns = Convert.ToInt32(Console.ReadLine().Trim());
  28. Console.WriteLine();
  29.  
  30. Console.WriteLine("Compatibility index of first column is: ");
  31. Console.WriteLine("{0}", CountCompatibilityIndex(0, divisionIntoColumns));
  32. Console.WriteLine();
  33.  
  34. Console.WriteLine("Set (new) division into columns to see all compatibility indexes: ");
  35. divisionIntoColumns = Convert.ToInt32(Console.ReadLine().Trim());
  36. List<double> allIndexes = CountAllCompatibilityIndexes(divisionIntoColumns);
  37. ShowAllIndexes(allIndexes);
  38.  
  39. }
  40.  
  41. #region Definitions
  42.  
  43. private static readonly Dictionary<char, int> ALPHABET = new Dictionary<char, int>()
  44. {
  45. {'a', 0}, {'b', 1}, {'c', 2}, {'d', 3}, {'e', 4}, {'f', 5}, {'g', 6}, {'h', 7}, {'i', 8},
  46. {'j', 9}, {'k', 10}, {'l', 11}, {'m', 12}, {'n', 13}, {'o', 14}, {'p', 15}, {'q', 16}, {'r', 17},
  47. {'s', 18}, {'t', 19}, {'u', 20}, {'v', 21}, {'w', 22}, {'x', 23}, {'y', 24}, {'z', 25}
  48. };
  49.  
  50. #endregion
  51.  
  52. #region Operations
  53.  
  54. #region I/O operations
  55.  
  56. private static string ReadTextFromFile(string path)
  57. {
  58.  
  59. StringBuilder text = new StringBuilder();
  60.  
  61. StreamReader streamReader = new StreamReader(path);
  62.  
  63. using (streamReader)
  64. {
  65.  
  66. string line = streamReader.ReadLine();
  67.  
  68. while (!string.IsNullOrEmpty(line))
  69. {
  70. text.Append(line);
  71. line = streamReader.ReadLine();
  72. }
  73.  
  74. }
  75.  
  76. return text.ToString();
  77.  
  78. }
  79.  
  80. private static void WriteTextToFile(string data, string path, bool mode = false)
  81. {
  82.  
  83. // mode determines if data is overriden (default) or appened
  84. StreamWriter streamWriter = new StreamWriter(path, mode);
  85.  
  86. using (streamWriter)
  87. {
  88. streamWriter.Write(data);
  89. }
  90.  
  91. }
  92.  
  93. #endregion
  94.  
  95. #region Vigenere encryption
  96.  
  97. private static string VigenereAlgorithm(string data, string key)
  98. {
  99.  
  100. StringBuilder encryptedData = new StringBuilder();
  101. char[] keyAsArr = key.ToCharArray();
  102. int index = 0;
  103. int encryptedChar;
  104.  
  105. foreach (char c in data)
  106. {
  107.  
  108. if (c == ' ')
  109. {
  110. encryptedData.Append(c);
  111. continue;
  112. }
  113.  
  114. if (index >= keyAsArr.Length)
  115. {
  116. index = 0;
  117. }
  118.  
  119. encryptedChar = c + ALPHABET[keyAsArr[index]];
  120.  
  121. if (encryptedChar >= 'z')
  122. {
  123. encryptedChar = encryptedChar - ALPHABET.Count;
  124. }
  125.  
  126. encryptedData.Append((char)encryptedChar);
  127.  
  128. index++;
  129.  
  130. }
  131.  
  132. return encryptedData.ToString();
  133.  
  134. }
  135.  
  136. #endregion
  137.  
  138. #region Compatibility index
  139.  
  140. private static char[,] ColumnDivision(string data, int NumberOfColumns)
  141. {
  142.  
  143. char[,] columnDivision = new char[data.Length / NumberOfColumns, NumberOfColumns];
  144. int counter = 0;
  145.  
  146. for (int i = 0; i < data.Length / NumberOfColumns; i++)
  147. {
  148.  
  149. for (int j = 0; j < NumberOfColumns; j++)
  150. {
  151. columnDivision[i, j] = data[counter];
  152. counter++;
  153. }
  154.  
  155. }
  156.  
  157. return columnDivision;
  158.  
  159. }
  160.  
  161. private static string GetDividedColumn(char[,] dividedColumns, int numberOfColumn)
  162. {
  163.  
  164. StringBuilder stringBuilder = new StringBuilder();
  165.  
  166. for (int i = 0; i < dividedColumns.GetLength(0); i++)
  167. {
  168. stringBuilder.Append(dividedColumns[i, numberOfColumn]);
  169. }
  170.  
  171. return stringBuilder.ToString();
  172.  
  173. }
  174.  
  175. private static double CompatibilityIndex(string data)
  176. {
  177.  
  178. double nominator = 0;
  179.  
  180. foreach (var letter in ALPHABET)
  181. {
  182.  
  183. double numberOfLetterRepeats = 0;
  184.  
  185. for (int i = 0; i < data.Length; i++)
  186. {
  187. if (letter.Key == data[i])
  188. {
  189. numberOfLetterRepeats++;
  190. }
  191. }
  192.  
  193. nominator += numberOfLetterRepeats * (numberOfLetterRepeats - 1);
  194.  
  195. }
  196.  
  197. return nominator / (data.Length * (data.Length - 1));
  198.  
  199. }
  200.  
  201. private static double CountCompatibilityIndex(int numberOfColumn, int divisionIntoColumns)
  202. {
  203.  
  204. char[,] columnDivision = ColumnDivision(ReadTextFromFile(outputPath), divisionIntoColumns);
  205. string firstColumnAfterDivision = GetDividedColumn(columnDivision, numberOfColumn);
  206. double compatibilityIndex = CompatibilityIndex(firstColumnAfterDivision);
  207.  
  208. return compatibilityIndex;
  209.  
  210. }
  211.  
  212. private static List<double> CountAllCompatibilityIndexes(int divisionIntoColumns)
  213. {
  214.  
  215. List<double> allCompatibilityIndexes = new List<double>();
  216.  
  217. char[,] columnDivision = ColumnDivision(ReadTextFromFile(outputPath), divisionIntoColumns);
  218.  
  219. string column = null;
  220. double compatibilityIndex = 0.0;
  221. for (int i = 0; i < columnDivision.GetLength(1); i++)
  222. {
  223.  
  224. column = GetDividedColumn(columnDivision, i);
  225. compatibilityIndex = CompatibilityIndex(column);
  226.  
  227. allCompatibilityIndexes.Add(compatibilityIndex);
  228.  
  229. }
  230.  
  231. return allCompatibilityIndexes;
  232.  
  233. }
  234.  
  235. #endregion
  236.  
  237. #region Present Data
  238.  
  239. private static void ShowAllIndexes(List<double> countedIndexes)
  240. {
  241.  
  242. Console.WriteLine("Compatibility indexes are:");
  243.  
  244. for (int i = 0; i < countedIndexes.Count; i++)
  245. {
  246. Console.WriteLine("Column number: {0}, its compatibility index: {1}", i, countedIndexes[i]);
  247. }
  248.  
  249. }
  250.  
  251. #endregion
  252.  
  253. #endregion
  254.  
  255. #region Data members
  256.  
  257. private static string key;
  258. private static int divisionIntoColumns;
  259. private static string inputPath;
  260. private static string outputPath;
  261.  
  262. #endregion
  263.  
  264. }
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement