Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace SzyfrVigenera
- {
- class Program
- {
- static void Main(string[] args)
- {
- inputPath = @"D:\Programowanie\SzyfrVigenera\SzyfrVigenera\tekst_Vigenere.txt";
- outputPath = @"D:\Programowanie\SzyfrVigenera\SzyfrVigenera\output.txt";
- Console.WriteLine("Set encryption key:");
- key = Console.ReadLine().Trim();
- string text = ReadTextFromFile(inputPath);
- string encryptedText = VigenereAlgorithm(text, key);
- WriteTextToFile(encryptedText, outputPath);
- Console.WriteLine();
- Console.WriteLine("Set division into columns: ");
- divisionIntoColumns = Convert.ToInt32(Console.ReadLine().Trim());
- Console.WriteLine();
- Console.WriteLine("Compatibility index of first column is: ");
- Console.WriteLine("{0}", CountCompatibilityIndex(0, divisionIntoColumns));
- Console.WriteLine();
- Console.WriteLine("Set (new) division into columns to see all compatibility indexes: ");
- divisionIntoColumns = Convert.ToInt32(Console.ReadLine().Trim());
- List<double> allIndexes = CountAllCompatibilityIndexes(divisionIntoColumns);
- ShowAllIndexes(allIndexes);
- }
- #region Definitions
- private static readonly Dictionary<char, int> ALPHABET = new Dictionary<char, int>()
- {
- {'a', 0}, {'b', 1}, {'c', 2}, {'d', 3}, {'e', 4}, {'f', 5}, {'g', 6}, {'h', 7}, {'i', 8},
- {'j', 9}, {'k', 10}, {'l', 11}, {'m', 12}, {'n', 13}, {'o', 14}, {'p', 15}, {'q', 16}, {'r', 17},
- {'s', 18}, {'t', 19}, {'u', 20}, {'v', 21}, {'w', 22}, {'x', 23}, {'y', 24}, {'z', 25}
- };
- #endregion
- #region Operations
- #region I/O operations
- private static string ReadTextFromFile(string path)
- {
- StringBuilder text = new StringBuilder();
- StreamReader streamReader = new StreamReader(path);
- using (streamReader)
- {
- string line = streamReader.ReadLine();
- while (!string.IsNullOrEmpty(line))
- {
- text.Append(line);
- line = streamReader.ReadLine();
- }
- }
- return text.ToString();
- }
- private static void WriteTextToFile(string data, string path, bool mode = false)
- {
- // mode determines if data is overriden (default) or appened
- StreamWriter streamWriter = new StreamWriter(path, mode);
- using (streamWriter)
- {
- streamWriter.Write(data);
- }
- }
- #endregion
- #region Vigenere encryption
- private static string VigenereAlgorithm(string data, string key)
- {
- StringBuilder encryptedData = new StringBuilder();
- char[] keyAsArr = key.ToCharArray();
- int index = 0;
- int encryptedChar;
- foreach (char c in data)
- {
- if (c == ' ')
- {
- encryptedData.Append(c);
- continue;
- }
- if (index >= keyAsArr.Length)
- {
- index = 0;
- }
- encryptedChar = c + ALPHABET[keyAsArr[index]];
- if (encryptedChar >= 'z')
- {
- encryptedChar = encryptedChar - ALPHABET.Count;
- }
- encryptedData.Append((char)encryptedChar);
- index++;
- }
- return encryptedData.ToString();
- }
- #endregion
- #region Compatibility index
- private static char[,] ColumnDivision(string data, int NumberOfColumns)
- {
- char[,] columnDivision = new char[data.Length / NumberOfColumns, NumberOfColumns];
- int counter = 0;
- for (int i = 0; i < data.Length / NumberOfColumns; i++)
- {
- for (int j = 0; j < NumberOfColumns; j++)
- {
- columnDivision[i, j] = data[counter];
- counter++;
- }
- }
- return columnDivision;
- }
- private static string GetDividedColumn(char[,] dividedColumns, int numberOfColumn)
- {
- StringBuilder stringBuilder = new StringBuilder();
- for (int i = 0; i < dividedColumns.GetLength(0); i++)
- {
- stringBuilder.Append(dividedColumns[i, numberOfColumn]);
- }
- return stringBuilder.ToString();
- }
- private static double CompatibilityIndex(string data)
- {
- double nominator = 0;
- foreach (var letter in ALPHABET)
- {
- double numberOfLetterRepeats = 0;
- for (int i = 0; i < data.Length; i++)
- {
- if (letter.Key == data[i])
- {
- numberOfLetterRepeats++;
- }
- }
- nominator += numberOfLetterRepeats * (numberOfLetterRepeats - 1);
- }
- return nominator / (data.Length * (data.Length - 1));
- }
- private static double CountCompatibilityIndex(int numberOfColumn, int divisionIntoColumns)
- {
- char[,] columnDivision = ColumnDivision(ReadTextFromFile(outputPath), divisionIntoColumns);
- string firstColumnAfterDivision = GetDividedColumn(columnDivision, numberOfColumn);
- double compatibilityIndex = CompatibilityIndex(firstColumnAfterDivision);
- return compatibilityIndex;
- }
- private static List<double> CountAllCompatibilityIndexes(int divisionIntoColumns)
- {
- List<double> allCompatibilityIndexes = new List<double>();
- char[,] columnDivision = ColumnDivision(ReadTextFromFile(outputPath), divisionIntoColumns);
- string column = null;
- double compatibilityIndex = 0.0;
- for (int i = 0; i < columnDivision.GetLength(1); i++)
- {
- column = GetDividedColumn(columnDivision, i);
- compatibilityIndex = CompatibilityIndex(column);
- allCompatibilityIndexes.Add(compatibilityIndex);
- }
- return allCompatibilityIndexes;
- }
- #endregion
- #region Present Data
- private static void ShowAllIndexes(List<double> countedIndexes)
- {
- Console.WriteLine("Compatibility indexes are:");
- for (int i = 0; i < countedIndexes.Count; i++)
- {
- Console.WriteLine("Column number: {0}, its compatibility index: {1}", i, countedIndexes[i]);
- }
- }
- #endregion
- #endregion
- #region Data members
- private static string key;
- private static int divisionIntoColumns;
- private static string inputPath;
- private static string outputPath;
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement