Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. class CaesarEncryption
  2.     {
  3.         public int Key { get; set; }
  4.         private char[] openAlphabet;
  5.         public char[] closeAlphabet;
  6.         public char[] frequencyAlphabet = new char[]
  7.         {
  8.             'о', 'н', 'а', 'и', 'т', 'в', 'е', 'р', 'і', 'с', 'к', 'м', 'д', 'л', 'у', 'п', 'я', 'з', 'ь', 'г', 'ч',
  9.             'б', 'х', 'ц', 'ю', 'ж', 'й', 'ї', 'є', 'ф', 'ш', 'щ'
  10.         };
  11.  
  12.         private string openText;
  13.         private string closeText;
  14.  
  15.         public CaesarEncryption()
  16.         {
  17.             openAlphabet = new[]
  18.             {
  19.                 'а', 'б', 'в', 'г', 'д', 'е', 'є', 'ж', 'з', 'и', 'і', 'ї', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с',
  20.                 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ю', 'я'
  21.             };
  22.             closeAlphabet = new char[openAlphabet.Length];
  23.             Key = 0;
  24.  
  25.         }
  26.  
  27.         private string RemovePunctuation(string Text)
  28.         {
  29.             Text = Text.ToLower();
  30.             StringBuilder NewText = new StringBuilder();
  31.             for (int i = 0; i < Text.Length; i++)
  32.             {
  33.                 if (Char.IsLetter(Text[i]))
  34.                 {
  35.                     NewText.Append(Text[i]);
  36.                 }
  37.             }
  38.  
  39.  
  40.             return NewText.ToString();
  41.         }
  42.  
  43.  
  44.  
  45.         public char[] alphabetOffset(int Key)
  46.         {
  47.  
  48.             closeAlphabet = openAlphabet.Clone() as char[];
  49.  
  50.             var tmp = closeAlphabet[closeAlphabet.Length - 1];
  51.  
  52.             for (int i = 0; i < Math.Abs(Key); i++)
  53.             {
  54.                
  55.                 if (Key > 0)
  56.                 {
  57.                     tmp = closeAlphabet[closeAlphabet.Length - 1];
  58.                     Array.Copy(closeAlphabet, 0, closeAlphabet, 1, closeAlphabet.Length - 1);
  59.                     closeAlphabet[0] = tmp;
  60.                 }
  61.                 else
  62.                 {
  63.                     tmp = closeAlphabet[0];
  64.                     Array.Copy(closeAlphabet, 1, closeAlphabet, 0, closeAlphabet.Length - 1);
  65.                     closeAlphabet[closeAlphabet.Length - 1] = tmp;
  66.                 }
  67.                            
  68.             }
  69.             return closeAlphabet;
  70.         }
  71.  
  72.         public string Encrypt(string OpenText)
  73.         {
  74.             OpenText = RemovePunctuation(OpenText);
  75.             int index;
  76.            
  77.             StringBuilder mycloseText = new StringBuilder(OpenText);
  78.              
  79.             for (int i = 0; i < OpenText.Length; i++)
  80.             {
  81.                
  82.                 index = Array.FindIndex(openAlphabet, p=> p==mycloseText[i]);
  83.                 mycloseText[i] = closeAlphabet[index];
  84.             }
  85.             closeText = mycloseText.ToString();
  86.  
  87.             return closeText;
  88.         }
  89.  
  90.         public string Decipher(string CloseText)
  91.         {
  92.             CloseText = RemovePunctuation(CloseText);
  93.             int index;
  94.             StringBuilder myopenText = new StringBuilder(CloseText);
  95.             for (int i = 0; i < CloseText.Length; i++)
  96.             {
  97.                 index = Array.FindIndex(closeAlphabet, p => p == myopenText[i]);
  98.                 myopenText[i] = openAlphabet[index];
  99.             }
  100.             openText = myopenText.ToString();
  101.             return openText;
  102.         }
  103.  
  104.         public List<string> Decrypt(string CloseText)
  105.         {
  106.             CloseText = RemovePunctuation(CloseText);
  107.             Dictionary<char, int> alphabet = new Dictionary<char, int>();
  108.  
  109.             foreach (var i in openAlphabet)
  110.             {
  111.                 alphabet[i] = 0;
  112.             }
  113.            
  114.             foreach (char i in CloseText)
  115.             {
  116.                 alphabet[i]++;
  117.             }
  118.  
  119.             var orderedAlphabet = alphabet.OrderBy(x => x.Value);
  120.  
  121.             int frequency = orderedAlphabet.First(p => true).Value; //value of the most popular letter in text
  122.            
  123.             int indexFr; //індекс зашифрованої літери яка найчастіше повтор.ється
  124.             int indexFrA; //індекс літери, яка найчастіше зустрічається в алфавіті
  125.  
  126.             List<string> result = new List<string>();
  127.  
  128.             foreach (var j in frequencyAlphabet)
  129.             {
  130.                 foreach (var i in orderedAlphabet)
  131.                 {
  132.                     if (i.Value == frequency)
  133.                     {
  134.                         indexFr = Array.FindIndex(openAlphabet, p => p == i.Key);
  135.                         indexFrA = Array.FindIndex(openAlphabet, p => p == j);
  136.                         alphabetOffset(indexFrA - indexFr);
  137.  
  138.                        
  139.  
  140.                     }
  141.                 }
  142.                 result.Add(Decipher(CloseText));
  143.             }    
  144.  
  145.             return result;
  146.         }
  147.  
  148.  
  149.  
  150.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement