示例#1
0
 /// <summary>
 /// Insert one line into document
 /// </summary>
 /// <param name="p">paragraph</param>
 /// <param name="phrase">text</param>
 /// <param name="foreground">foreground color</param>
 /// <param name="underlined">true if underlined</param>
 private void InsertPhraseIntoDocument(Paragraph p, string phrase, System.Windows.Media.Brush foreground, bool underlined = false)
 {
     foreach (string s in phrase.SplitForTex(this.DelimiterLeft, this.DelimiterRight))
     {
         if (s.StartsWith(this.DelimiterLeft.ToString()) && s.EndsWith(this.DelimiterRight.ToString()))
         {
             string tex = s.Substring(1, s.Length - 2);
             WpfMath.Controls.FormulaControl fc = new WpfMath.Controls.FormulaControl();
             fc.Formula = tex;
             InlineUIContainer i = new InlineUIContainer(fc);
             if (foreground != null)
             {
                 i.Foreground = foreground;
             }
             p.Inlines.Add(i);
         }
         else
         {
             Run t = new Run(s);
             if (foreground != null)
             {
                 t.Foreground = foreground;
             }
             if (underlined)
             {
                 p.Inlines.Add(new Underline(t));
             }
             else
             {
                 p.Inlines.Add(t);
             }
         }
     }
 }
示例#2
0
 /// <summary>
 /// Insert one line into document
 /// </summary>
 /// <param name="p">paragraph</param>
 /// <param name="phrase">text</param>
 /// <param name="foreground">foreground color</param>
 private void InsertPhraseIntoDocument(Paragraph p, string phrase, System.Windows.Media.Brush foreground)
 {
     foreach (string s in phrase.SplitForTex('{', '}'))
     {
         if (s.StartsWith("{") && s.EndsWith("}"))
         {
             string tex = s.Substring(1, s.Length - 2);
             WpfMath.Controls.FormulaControl fc = new WpfMath.Controls.FormulaControl();
             fc.Formula = tex;
             InlineUIContainer i = new InlineUIContainer(fc);
             if (foreground != null)
             {
                 i.Foreground = foreground;
             }
             p.Inlines.Add(i);
         }
         else
         {
             Run t = new Run(s);
             if (foreground != null)
             {
                 t.Foreground = foreground;
             }
             p.Inlines.Add(t);
         }
     }
 }
示例#3
0
        protected static Inline FormulaToInline(string formula)
        {
            var contain = new InlineUIContainer();
            var fobj    = new WpfMath.Controls.FormulaControl();

            contain.Child             = fobj;
            contain.BaselineAlignment = System.Windows.BaselineAlignment.Center;
            contain.Foreground        = Brushes.White;
            fobj.Foreground           = Brushes.White;
            fobj.Margin    = new System.Windows.Thickness(4, 4, 2, 2);
            fobj.MinHeight = 4;
            fobj.MinWidth  = 4;
            fobj.Formula   = formula;
            return(contain);
        }
示例#4
0
 /// <summary>
 /// Insert text elements into a list
 /// </summary>
 /// <param name="s">owner</param>
 /// <param name="delimOn">delimiter on</param>
 /// <param name="delimOff">deliiter off</param>
 /// <param name="p">paragraph</param>
 public static void InsertIntoDocument(this string s, char delimOn, char delimOff, Paragraph p)
 {
     foreach (string c in s.SplitForTex(delimOn, delimOff))
     {
         if (c.StartsWith(delimOn.ToString()) && c.EndsWith(delimOff.ToString()))
         {
             string tex = c.Substring(1, c.Length - 2);
             WpfMath.Controls.FormulaControl fc = new WpfMath.Controls.FormulaControl();
             fc.Formula = tex;
             p.Inlines.Add(new InlineUIContainer(fc));
         }
         else
         {
             p.Inlines.Add(new Run(c));
         }
     }
 }
示例#5
0
        private WpfMath.TexFormula ParseFormula(string input)
        {
            // Create formula object from input text.
            WpfMath.TexFormula formula = null;
            try
            {
                formula = formulaParser.Parse(input);
            }
            catch (Exception ex)
            {
                //System.Windows.MessageBox.Show("An error occurred while parsing the given input:" + Environment.NewLine +
                //    Environment.NewLine + ex.Message, "Accessible EPUB", MessageBoxButton.OK, MessageBoxImage.Error);
                System.Windows.MessageBox.Show(Resource_MessageBox.parseInputContent + Environment.NewLine +
                                               Environment.NewLine + ex.Message, "Accessible EPUB", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return(formula);
        }
示例#6
0
        public MagicFormula_TexEquation()
        {
            InitializeComponent();


            for (int i = elementHost.Count() - 1; i >= 0; --i)
            {
                formulaControl[i]         = new WpfMath.Controls.FormulaControl();
                formulaControl[i].Formula = FY[i];
                elementHost[i]            = new System.Windows.Forms.Integration.ElementHost();
                //コントロールの位置と大きさを設定する
                elementHost[i].Dock  = DockStyle.Top;
                elementHost[i].Child = formulaControl[i];

                //ElementHostをフォームに配置する
                this.Controls.Add(elementHost[i]);
            }
        }
示例#7
0
        public NumberCipherWindow(Client Alice, Client Bob)
        {
            this.Alice = Alice;
            this.Bob   = Bob;
            ContextDataNumberCipherWindow cd = new ContextDataNumberCipherWindow
            {
                MainFormula         = @"(t*g^{ab}, g^{a})",
                MainDecipherFormula = @"(t*g^{ab}) * (g^{a})^{-b} = t"
            };

            InitializeComponent();
            DataContext = cd;

            StackPanel sp;
            string     message = NumberLibrary.Data.Message;

            SourceParts     = new List <string>(); // Исходные куски сообщения
            EncodedParts    = new List <string>(); // Закодированные куски
            CipheredParts   = new List <string>(); // Зашифрованные куски
            DecipheredParts = new List <string>(); // Дешифрованные куски
            RelaxedPartes   = new List <string>(); // Зашифрованные в текстовом виде куски

            // В этом цикле создается вся информация выше (т.е. приведение данных к читабельному виду)
            for (int i = 0; i < message.Length / 5 + CheckDivisionBy5(message.Length); i++)
            {
                bool flag = false;
                int  size = 5;
                if (message.Length - i * 5 < 5)
                {
                    size = message.Length - i * 5;
                    flag = true;
                }
                int dimOfPrint = (!flag) ? 5 : message.Length % 5; // Какой кусочек нужно восстановить

                SourceParts.Add(message.Substring(i * 5, size));   // Исходные куски
                BigInteger cash = NumberLibrary.Data.Encode(message.Substring(i * 5, size));
                EncodedParts.Add(cash.ToString());                 // Закодированные куски

                Alice.SentMessage = message.Substring(i * 5, size);
                Alice.TransportTo(Bob);

                Bob.Decipher();                                                                 // Выполним дешифрование
                CipheredParts.Add(Bob.CipheredNumber.ToString());
                DecipheredParts.Add(NumberLibrary.Data.Encode(Bob.RecievedMessage).ToString()); // Дешифрованные куски
                RelaxedPartes.Add(Bob.RecievedMessage);
            }

            // В этом цикле передается кодирование
            for (int i = 0; i < SourceParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };
                Label med = new Label {
                    Content = SourceParts[i], FontSize = 23
                };
                MainWindow.HandleMargin(med, 15);
                MainWindow.AddChildren(sp, med);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, -6, 20);

                MainWindow.AddChildren(sp, myImage);

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = NumberLibrary.Data.EncodeAndConvert(SourceParts[i]) + $"= {EncodedParts[i]} = t_{{{i+1}}}"
                };
                MainWindow.HandleMargin(fml, 0, 8);
                MainWindow.AddChildren(sp, fml);

                MainWindow.AddChildren(EncodeStackPanel, sp);
            }

            // Здесь шифруются куски
            for (int i = 0; i < EncodedParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t_{{{i + 1}}} =" + EncodedParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 22);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 5, 20);
                MainWindow.AddChildren(sp, myImage);

                fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"(t_{{{i + 1}}} * g^{{ab}})mod(p)=({EncodedParts[i]}*{Client.CommonKey}) mod ({NumberLibrary.Data.Modulus})={CipheredParts[i]}=t'_{{{i + 1}}}"
                };
                MainWindow.HandleMargin(fml, 0, 16);
                MainWindow.AddChildren(sp, fml);

                MainWindow.AddChildren(TransformStackPanel, sp);
            }

            // Здесь зашифрованные куски идут на вывод в текстовом виде;
            for (int i = 0; i < CipheredParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t'_{{{i + 1}}} = " + CipheredParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 20);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 6, 20);
                MainWindow.AddChildren(sp, myImage);

                Label med = new Label {
                    Content = NumberLibrary.Data.Decode(BigInteger.Parse(CipheredParts[i])), FontSize = 20
                };
                MainWindow.HandleMargin(med, 0, 12);

                MainWindow.AddChildren(sp, med);
                MainWindow.AddChildren(CipheredStackPanel, sp);
            }

            Exchange();
        }
示例#8
0
        public NumberDecipherWindow(Client Alice, Client Bob)
        {
            this.Alice = Alice;
            this.Bob   = Bob;



            // Инициализируем необходимы DataContext;
            ContextDataNumberDecipherWindow cd = new ContextDataNumberDecipherWindow
            {
                Gabo = @"g^{-ab}(x)=" + $"({Client.CommonKey})^{{-1}}={Client.InversedCommonKey}"
            };


            InitializeComponent();
            DataContext = cd;

            StackPanel sp;

            // Кодирование зашифрованных текстовых кусочков
            for (int i = 0; i < NumberCipherWindow.CipheredParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                Label med = new Label {
                    Content = Data.Decode(BigInteger.Parse(NumberCipherWindow.CipheredParts[i])), FontSize = 20
                };
                MainWindow.HandleMargin(med, 20);
                MainWindow.AddChildren(sp, med);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, -7, 20);

                MainWindow.AddChildren(sp, myImage);

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = NumberCipherWindow.CipheredParts[i] + $" = t'_{i + 1}(x)"
                };
                MainWindow.HandleMargin(fml, 0, 7);
                MainWindow.AddChildren(sp, fml);
                MainWindow.AddChildren(EncodeStackPanel, sp);
            }

            for (int i = 0; i < NumberCipherWindow.EncodedParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t'_{i + 1}(x) = " + NumberCipherWindow.CipheredParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 20);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 5, 20);
                MainWindow.AddChildren(sp, myImage);

                fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"(t'_{{{i+1}}}*g^{{-ab}})mod(p)=({NumberCipherWindow.CipheredParts[i]}*{Client.InversedCommonKey}) mod ({Data.Modulus})={NumberCipherWindow.DecipheredParts[i]}=t_{{{i + 1}}}(x)"
                };
                MainWindow.HandleMargin(fml, 0, 18);
                MainWindow.AddChildren(sp, fml);

                MainWindow.AddChildren(DecipherStackPanel, sp);
            }

            // Восстановление дешифрованных кусков
            for (int i = 0; i < NumberCipherWindow.SourceParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t_{{{i + 1}}}(x) = " + NumberCipherWindow.EncodedParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 20);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 4, 20);
                MainWindow.AddChildren(sp, myImage);
                Label med = new Label {
                    Content = NumberCipherWindow.SourceParts[i], FontSize = 23
                };
                MainWindow.HandleMargin(med, 0, 8);
                MainWindow.AddChildren(sp, med);

                MainWindow.AddChildren(DecodeStackPanel, sp);
            }

            MainWindow.AddChildren(MainPanel, new Label {
                FontSize = 25, Content = Data.Message
            });
        }
示例#9
0
        public PolyDecipherWindow(Account Alice, Account Bob)
        {
            this.Alice = Alice;
            this.Bob   = Bob;

            // Обратный многочлен, на который умножается зашифрованное слово
            string invRepresentation = MainWindow.PolynomialToString(Account.InversedPower);

            // Инициализируем необходимы DataContext;
            ContextDataDecipherWindow cd = new ContextDataDecipherWindow
            {
                Gabo = @"g^{-ab}(x)=" + $"({Account.CommonKey})^{{-1}}={invRepresentation}"
            };


            InitializeComponent();
            DataContext = cd;

            StackPanel sp;

            // Кодирование зашифрованных текстовых кусочков
            for (int i = 0; i < PolyCipherWindow2.CipheredParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                Label med = new Label {
                    Content = PolyCipherWindow2.RelaxedPartes[i], FontSize = 20
                };
                MainWindow.HandleMargin(med, 20);
                MainWindow.AddChildren(sp, med);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, -7, 20);

                MainWindow.AddChildren(sp, myImage);

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = PolyCipherWindow2.CipheredParts[i] + $" = t'_{i + 1}(x)"
                };
                MainWindow.HandleMargin(fml, 0, 7);
                MainWindow.AddChildren(sp, fml);
                MainWindow.AddChildren(EncodeStackPanel, sp);
            }

            for (int i = 0; i < PolyCipherWindow2.EncodedParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t'_{i + 1}(x) = " + PolyCipherWindow2.CipheredParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 20);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 5, 20);
                MainWindow.AddChildren(sp, myImage);

                string md = MainWindow.PolynomialToString(Data.FieldOfCryptosystem.Modulus);
                fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"(({PolyCipherWindow2.CipheredParts[i]})*({invRepresentation})) mod ({md})={PolyCipherWindow2.DecipheredParts[i]}=t_{{{i + 1}}}(x)"
                };
                MainWindow.HandleMargin(fml, 0, 18);
                MainWindow.AddChildren(sp, fml);

                MainWindow.AddChildren(DecipherStackPanel, sp);
            }

            // Восстановление дешифрованных кусков
            for (int i = 0; i < PolyCipherWindow2.SourceParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t_{{{i + 1}}}(x) = " + PolyCipherWindow2.EncodedParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 20);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 4, 20);
                MainWindow.AddChildren(sp, myImage);
                Label med = new Label {
                    Content = PolyCipherWindow2.SourceParts[i], FontSize = 23
                };
                MainWindow.HandleMargin(med, 0, 8);
                MainWindow.AddChildren(sp, med);

                MainWindow.AddChildren(DecodeStackPanel, sp);
            }

            MainWindow.AddChildren(MainPanel, new Label {
                FontSize = 25, Content = Data.Message
            });
        }
        public NumberInfoWindow(Client A, Client B)
        {
            Alice = A; Bob = B;
            Client.CreateCommonKey(Bob.OpenNumber, Alice.PrivatePower);
            Client.CreateInversedCommonKey();

            collection = new ObservableCollection <Item>();
            // Инициализируем необходимы DataContext;
            ContextDataNumberInfo cd = new ContextDataNumberInfo
            {
                A   = "a = " + Convert.ToString(Alice.PrivatePower),
                B   = "b = " + Convert.ToString(Bob.PrivatePower),
                Ga  = "g^{a} = " + Alice.OpenNumber,
                Gb  = "g^{b} = " + Bob.OpenNumber,
                Gab = "g^{ab} = " + Client.CommonKey,
                p   = "p = " + Data.Modulus,
                g   = "g = " + Data.PublicNumber
            };

            InitializeComponent();
            DGrid.ItemsSource = collection;

            foreach (KeyValuePair <char, int> pr in Data.codesOfSymbols)
            {
                collection.Add(new Item
                {
                    Symbol = Convert.ToString(pr.Key),
                    Code   = Convert.ToString(pr.Value)
                });
            }

            // Теперь необходимо вывести необходимые операции для возведения многочлена в степень

            List <int> AlicePowers = new List <int>();
            int        a           = Alice.PrivatePower;

            while (a > 0)
            {
                AlicePowers.Add(a);
                a /= 2;
            }
            AlicePowers.Reverse();
            for (int i = 0; i < AlicePowers.Count; i++)
            {
                string p = FormPrefix(AlicePowers[i]); // Начальный префикс выводимой строки

                string pres = Alice.BasicPowers[i].ToString();
                if (AlicePowers[i] % 2 == 0 && AlicePowers[i] != 1) // Добавляем некоторый префикс перед выводом
                {
                    p += $" = (({pres})*({pres})) mod ({Data.Modulus.ToString()}) = ";
                }
                else if (AlicePowers[i] != 1)
                {
                    p += $" = (({Data.PublicNumber.ToString()}))*({pres})^{{2}}) mod ({Data.Modulus.ToString()}) = ";
                }
                p += Alice.BasicPowers[i + 1].ToString(); // Добавить самое значение выражения
                WpfMath.Controls.FormulaControl fc = MainWindow.Formula(p);
                MainWindow.HandleMargin(fc, 15);
                MainWindow.AddChildren(AliceStackPanel, fc);
            }

            List <int> BobPowers = new List <int>();
            int        b         = Bob.PrivatePower;

            while (b > 0)
            {
                BobPowers.Add(b);
                b /= 2;
            }
            BobPowers.Reverse();
            for (int i = 0; i < BobPowers.Count; i++)
            {
                string p = FormPrefix(BobPowers[i]);

                string pres = Bob.BasicPowers[i].ToString();
                if (BobPowers[i] % 2 == 0 && BobPowers[i] != 1) // Необходимый префикс для удобчитаемого вида
                {
                    p += $" = (({pres})*({pres})) mod ({Data.Modulus}) = ";
                }
                else if (BobPowers[i] != 1)
                {
                    p += $" = (({Data.PublicNumber})*({pres})^{{2}}) mod ({Data.Modulus}) = ";
                }
                p += Bob.BasicPowers[i + 1]; // Добавить самое значение выражения
                WpfMath.Controls.FormulaControl fc = MainWindow.Formula(p);
                MainWindow.HandleMargin(fc, 15);
                MainWindow.AddChildren(BobStackPanel, fc);
            }

            DataContext = cd;
        }
示例#11
0
        public PolyCipherWindow2(Account Alice, Account Bob)
        {
            this.Alice = Alice;
            this.Bob   = Bob;
            ContextDataCipher2Window cd = new ContextDataCipher2Window
            {
                MainFormula         = @"(t*g^{ab}, g^{a})",
                MainDecipherFormula = @"(t*g^{ab}) * (g^{a})^{-b} = t"
            };

            InitializeComponent();
            DataContext = cd;
            StackPanel sp;
            string     message = Data.Message;

            SourceParts         = new List <string>(); // Исходные куски сообщения
            EncodedParts        = new List <string>(); // Закодированные куски
            CipheredParts       = new List <string>(); // Зашифрованные куски
            OutputCipheredParts = new List <string>();
            PolyCipheredParts   = new List <Polynomial>();
            DecipheredParts     = new List <string>(); // Дешифрованные куски
            RelaxedPartes       = new List <string>(); // Зашифрованные в текстовом виде куски
            PolyDecipheredParts = new List <Polynomial>();

            // В этом цикле создается вся информация выше (т.е. приведение данных к читабельному виду)
            for (int i = 0; i < message.Length / 5 + CheckDivisionBy5(message.Length); i++)
            {
                bool flag = false;
                int  size = 5;
                if (message.Length - i * 5 < 5)
                {
                    size = message.Length - i * 5;
                    flag = true;
                }
                int dimOfPrint = (!flag) ? 5 : message.Length % 5; // Какой кусочек нужно восстановить

                SourceParts.Add(message.Substring(i * 5, size));
                Polynomial cash = Data.StringToPolynomial(message.Substring(i * 5, size));
                EncodedParts.Add(MainWindow.PolynomialToString(cash));

                Alice.CreateMessage(message.Substring(i * 5, size));
                Alice.TransportTo(Bob);

                Bob.MessagePolynomial = Bob.Decipher();

                Bob.EncryptedMessage = Data.CheckIt(Bob.EncryptedMessage);

                Polynomial DecipheredPolynomial = Normalize(Bob.Decipher(), dimOfPrint);
                DecipheredParts.Add(MainWindow.PolynomialToString(DecipheredPolynomial));

                CipheredParts.Add(MainWindow.PolynomialToString(Bob.EncryptedMessage));
                OutputCipheredParts.Add(Bob.EncryptedMessage.ToString());
                PolyCipheredParts.Add(Bob.EncryptedMessage);
                RelaxedPartes.Add(GetEncryptedMessage(Bob.EncryptedMessage, flag, message));
                PolyDecipheredParts.Add(Bob.MessagePolynomial);
            }

            // В этом цикле передается кодирование
            for (int i = 0; i < SourceParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };
                Label med = new Label {
                    Content = SourceParts[i], FontSize = 23
                };
                MainWindow.HandleMargin(med, 15);
                MainWindow.AddChildren(sp, med);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, -6, 20);

                MainWindow.AddChildren(sp, myImage);

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = EncodedParts[i] + $"= t_{{{i + 1}}}(x)"
                };
                MainWindow.HandleMargin(fml, 0, 6);
                MainWindow.AddChildren(sp, fml);

                MainWindow.AddChildren(EncodeStackPanel, sp);
            }

            // Создаем общий многочлен для вывода в будущем и для поиска обратного
            Polynomial commonPoly = Data.Modulus(Polynomial.Power(Alice.publicPolynomial, Bob.privatePower,
                                                                  Data.FieldOfCryptosystem.Order),
                                                 Data.FieldOfCryptosystem.Modulus, Data.FieldOfCryptosystem.Order);

            Account.CommonKey = MainWindow.PolynomialToString(commonPoly);

            // Здесь шифруются куски
            for (int i = 0; i < EncodedParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t_{{{i + 1}}}(x) = " + EncodedParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 20);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 6, 20);
                MainWindow.AddChildren(sp, myImage);

                fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t_{{{i + 1}}}(x) * g^{{ab}}(x)=({EncodedParts[i]})*({Account.CommonKey})={CipheredParts[i]}" + $"=t'_{i+1}(x)"
                };
                MainWindow.HandleMargin(fml, 0, 16);
                MainWindow.AddChildren(sp, fml);

                MainWindow.AddChildren(TransformStackPanel, sp);
            }

            // Здесь зашифрованные куски идут на вывод в текстовом виде;
            for (int i = 0; i < CipheredParts.Count; i++)
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };

                WpfMath.Controls.FormulaControl fml = new WpfMath.Controls.FormulaControl
                {
                    FontSize = 30,
                    Formula  = $"t'_{i+1}(x) = " + CipheredParts[i]
                };
                MainWindow.HandleMargin(fml, 20, 20);
                MainWindow.AddChildren(sp, fml);

                Image       myImage = new Image();
                BitmapImage bi      = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri("Images/Arrow.png", UriKind.Relative);
                bi.EndInit();
                myImage.Source  = bi;
                myImage.Width   = 50;
                myImage.Height  = 50;
                myImage.Stretch = Stretch.Fill;
                MainWindow.HandleMargin(myImage, 20, 6, 20);
                MainWindow.AddChildren(sp, myImage);

                Label med = new Label {
                    Content = RelaxedPartes[i], FontSize = 20
                };
                MainWindow.HandleMargin(med, 0, 14);

                MainWindow.AddChildren(sp, med);
                MainWindow.AddChildren(CipheredStackPanel, sp);
            }
        }
        public PolyCipherWindow(Account A, Account B)
        {
            Alice = A; Bob = B;

            collection = new ObservableCollection <Item>();

            // Вспомогательный класс, хранящий в себе формулы, которые необходимы отобразить
            ContextDataCipherWindow cd = new ContextDataCipherWindow
            {
                A       = "a = " + Convert.ToString(Alice.privatePower),
                B       = "b = " + Convert.ToString(Bob.privatePower),
                Ga      = "g^{a}(x) = " + MainWindow.PolynomialToString(Alice.publicPolynomial),
                Gb      = "g^{b}(x) = " + MainWindow.PolynomialToString(Bob.publicPolynomial),
                GaPower = "g^{a}(x) = " + Alice.MessagePowerPolynomial.Replace("(g)", "g"),
                GbPower = "g^{b}(x) = " + Bob.MessagePowerPolynomial.Replace("(g)", "g"),
                Gab     = "g^{ab}(x) = " + MainWindow.PolynomialToString(Data.Modulus(Polynomial.Power(Alice.publicPolynomial, Bob.privatePower,
                                                                                                       Data.FieldOfCryptosystem.Order),
                                                                                      Data.FieldOfCryptosystem.Modulus, Data.FieldOfCryptosystem.Order))
            };

            InitializeComponent();
            DGrid.ItemsSource = collection;

            foreach (KeyValuePair <char, int> pr in Data.codesOfSymbols)
            {
                collection.Add(new Item
                {
                    Symbol = Convert.ToString(pr.Key),
                    Code   = Convert.ToString(pr.Value)
                });
            }

            // Теперь необходимо вывести необходимые операции для возведения многочлена в степень
            List <int> AlicePowers = new List <int>(); // Эти степени необходимы, т.к. именно они вычисляются
            //в методе быстрого возведения в квадрат
            int a = Alice.privatePower;

            while (a > 0)
            {
                AlicePowers.Add(a);
                a /= 2;
            }
            AlicePowers.Reverse();
            Polynomial g     = Data.FieldOfCryptosystem.OpenPolynomial;
            int        order = Data.FieldOfCryptosystem.Order;

            MainWindow.HandleMargin(AliceStackPanel, 15);
            MainWindow.HandleMargin(BobStackPanel, 15);
            try
            {
                for (int i = 0; i < AlicePowers.Count; i++)
                {
                    string p = FormPrefix(AlicePowers[i]);

                    string pres = MainWindow.PolynomialToString(Alice.BasicPowers[i]);
                    if (AlicePowers[i] % 2 == 0 && AlicePowers[i] != 1) // Нужно сформировать конкретное выражение для вычисления
                    {
                        p += $" = (({pres})*({pres})) mod ({MainWindow.PolynomialToString(Data.FieldOfCryptosystem.Modulus)}) = ";
                    }
                    else if (AlicePowers[i] != 1)
                    {
                        p += $" = (({MainWindow.PolynomialToString(g)})*({pres})^{{2}}) mod ({MainWindow.PolynomialToString(Data.FieldOfCryptosystem.Modulus)}) = ";
                    }
                    p += MainWindow.PolynomialToString(Bob.BasicPowers[i + 1]); // Добавим в конец результат, чтобы узнать, чему равняется текущая степень
                    Thread.Sleep(300);
                    WpfMath.Controls.FormulaControl f = new WpfMath.Controls.FormulaControl()
                    {
                        Formula = p
                    };
                    Thread.Sleep(300);
                    MainWindow.AddChildren(AliceStackPanel, f);
                }
                List <int> BobPowers = new List <int>(); // Эти степени необходимы, т.к. именно они вычисляются
                                                         //в методе быстрого возведения в квадрат
                int b = Bob.privatePower;
                while (b > 0)
                {
                    BobPowers.Add(b);
                    b /= 2;
                }
                BobPowers.Reverse();
                for (int i = 0; i < BobPowers.Count; i++)
                {
                    string p = FormPrefix(BobPowers[i]);

                    string pres = MainWindow.PolynomialToString(Bob.BasicPowers[i]);
                    if (BobPowers[i] % 2 == 0 && BobPowers[i] != 1) // Нужно сформировать конкретное выражение для вычисления
                    {
                        p += $" = (({pres})*({pres})) mod ({MainWindow.PolynomialToString(Data.FieldOfCryptosystem.Modulus)}) = ";
                    }
                    else if (BobPowers[i] != 1)
                    {
                        p += $" = (({MainWindow.PolynomialToString(g)})*({pres})^{{2}}) mod ({MainWindow.PolynomialToString(Data.FieldOfCryptosystem.Modulus)}) = ";
                    }
                    p += MainWindow.PolynomialToString(Bob.BasicPowers[i + 1]); // Добавим в конец результат, чтобы узнать, чему равняется текущая степень
                    Thread.Sleep(300);
                    WpfMath.Controls.FormulaControl f = new WpfMath.Controls.FormulaControl()
                    {
                        Formula = p
                    };
                    Thread.Sleep(300);
                    MainWindow.AddChildren(BobStackPanel, f);
                }
            } catch (Exception)
            {
                PolyDisplayWindow win = new PolyDisplayWindow()
                {
                    Top  = Top,
                    Left = Left
                };
            }
            DataContext = cd;
        }