示例#1
0
        private void EncryptButton_Click(object sender, RoutedEventArgs e)
        {
            if (BaseStateField.IsMaskCompleted && CheckRegisterState(BaseStateField.Text) &&
                BaseContentField.Text.Trim() != string.Empty)
            {
                LFSRCipher cipher = new LFSRCipher(Convert.ToInt32(BaseStateField.Text.Substring(2), 2));

                long length = BaseContentField.Text.Length / 2;

                cipher.CountKey(length);

                string result = string.Empty;
                string key    = string.Empty;
                for (long i = 0; i < length; i++)
                {
                    byte b = Convert.ToByte(BaseContentField.Text.Substring((int)(i * 2), 2), 16);
                    key += Convert.ToString(cipher.Key[i], 2);

                    string buff = Convert.ToString(b ^ cipher.Key[i], 16);
                    while (buff.Length < 2)
                    {
                        buff = buff.Insert(0, "0");
                    }
                    result += buff;
                }
                KeyField.Text = key;

                ResultField.Text = result;
            }
            else
            {
                Console.WriteLine("Error");
            }
        }
示例#2
0
        private void DecryptButton_Click(object sender, RoutedEventArgs e)
        {
            if (BaseStateField.IsMaskCompleted && CheckRegisterState(BaseStateField.Text) &&
                BaseContentField.Text.Trim() != string.Empty)
            {
                LFSRCipher cipher = new LFSRCipher(Convert.ToInt32(BaseStateField.Text.Substring(2), 2));

                long length = BaseContentField.Text.Length / 2;

                cipher.CountKey(length);

                string result = string.Empty;
                string key    = string.Empty;
                for (long i = 0; i < length; i++)
                {
                    byte b = Convert.ToByte(BaseContentField.Text.Substring((int)(i * 2), 2), 16);
                    key += Convert.ToString(cipher.Key[i], 2);

                    string buff = Convert.ToString(b ^ cipher.Key[i], 16);
                    while (buff.Length < 2)
                    {
                        buff = buff.Insert(0, "0");
                    }
                    result += buff;
                }
                KeyField.Text = key;

                ResultField.Text = result;

                if (MessageBox.Show("Сохранить результат как файл?", "Сохранить?", MessageBoxButton.YesNo) ==
                    MessageBoxResult.Yes)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    if (saveFileDialog.ShowDialog() == true)
                    {
                        using (FileStream fileStream = new FileStream(saveFileDialog.FileName,
                                                                      FileMode.Create, FileAccess.Write))
                        {
                            for (int i = 0; i < length; i++)
                            {
                                fileStream.WriteByte(Convert.ToByte(result.Substring(i * 2, 2), 16));
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Error");
            }
        }