Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CaesarEncryption
- {
- public int Key { get; set; }
- private char[] openAlphabet;
- public char[] closeAlphabet;
- public char[] frequencyAlphabet = new char[]
- {
- 'о', 'н', 'а', 'и', 'т', 'в', 'е', 'р', 'і', 'с', 'к', 'м', 'д', 'л', 'у', 'п', 'я', 'з', 'ь', 'г', 'ч',
- 'б', 'х', 'ц', 'ю', 'ж', 'й', 'ї', 'є', 'ф', 'ш', 'щ'
- };
- private string openText;
- private string closeText;
- public CaesarEncryption()
- {
- openAlphabet = new[]
- {
- 'а', 'б', 'в', 'г', 'д', 'е', 'є', 'ж', 'з', 'и', 'і', 'ї', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с',
- 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ю', 'я'
- };
- closeAlphabet = new char[openAlphabet.Length];
- Key = 0;
- }
- private string RemovePunctuation(string Text)
- {
- Text = Text.ToLower();
- StringBuilder NewText = new StringBuilder();
- for (int i = 0; i < Text.Length; i++)
- {
- if (Char.IsLetter(Text[i]))
- {
- NewText.Append(Text[i]);
- }
- }
- return NewText.ToString();
- }
- public char[] alphabetOffset(int Key)
- {
- closeAlphabet = openAlphabet.Clone() as char[];
- var tmp = closeAlphabet[closeAlphabet.Length - 1];
- for (int i = 0; i < Math.Abs(Key); i++)
- {
- if (Key > 0)
- {
- tmp = closeAlphabet[closeAlphabet.Length - 1];
- Array.Copy(closeAlphabet, 0, closeAlphabet, 1, closeAlphabet.Length - 1);
- closeAlphabet[0] = tmp;
- }
- else
- {
- tmp = closeAlphabet[0];
- Array.Copy(closeAlphabet, 1, closeAlphabet, 0, closeAlphabet.Length - 1);
- closeAlphabet[closeAlphabet.Length - 1] = tmp;
- }
- }
- return closeAlphabet;
- }
- public string Encrypt(string OpenText)
- {
- OpenText = RemovePunctuation(OpenText);
- int index;
- StringBuilder mycloseText = new StringBuilder(OpenText);
- for (int i = 0; i < OpenText.Length; i++)
- {
- index = Array.FindIndex(openAlphabet, p=> p==mycloseText[i]);
- mycloseText[i] = closeAlphabet[index];
- }
- closeText = mycloseText.ToString();
- return closeText;
- }
- public string Decipher(string CloseText)
- {
- CloseText = RemovePunctuation(CloseText);
- int index;
- StringBuilder myopenText = new StringBuilder(CloseText);
- for (int i = 0; i < CloseText.Length; i++)
- {
- index = Array.FindIndex(closeAlphabet, p => p == myopenText[i]);
- myopenText[i] = openAlphabet[index];
- }
- openText = myopenText.ToString();
- return openText;
- }
- public List<string> Decrypt(string CloseText)
- {
- CloseText = RemovePunctuation(CloseText);
- Dictionary<char, int> alphabet = new Dictionary<char, int>();
- foreach (var i in openAlphabet)
- {
- alphabet[i] = 0;
- }
- foreach (char i in CloseText)
- {
- alphabet[i]++;
- }
- var orderedAlphabet = alphabet.OrderBy(x => x.Value);
- int frequency = orderedAlphabet.First(p => true).Value; //value of the most popular letter in text
- int indexFr; //індекс зашифрованої літери яка найчастіше повтор.ється
- int indexFrA; //індекс літери, яка найчастіше зустрічається в алфавіті
- List<string> result = new List<string>();
- foreach (var j in frequencyAlphabet)
- {
- foreach (var i in orderedAlphabet)
- {
- if (i.Value == frequency)
- {
- indexFr = Array.FindIndex(openAlphabet, p => p == i.Key);
- indexFrA = Array.FindIndex(openAlphabet, p => p == j);
- alphabetOffset(indexFrA - indexFr);
- }
- }
- result.Add(Decipher(CloseText));
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement