예제 #1
0
        public IActionResult SelfDecimationGenerate([FromBody] LfsrGeneratorsViewModel viewModel)
        {
            var numOfRegisters = viewModel.Registers.Length;

            Lfsr[] registers = new Lfsr[numOfRegisters];

            for (int i = 0; i < numOfRegisters; i++)
            {
                registers[i] = new Lfsr(viewModel.Registers[i]);
                registers[i].SetFeedbackFunction(viewModel.FeedbackFunctions[i]);
            }

            SelfDecimationGenerator generator = new SelfDecimationGenerator(registers);

            generator.K_value = (int)viewModel.K_value;
            generator.D_value = (int)viewModel.D_value;

            SeriesMode mode = (SeriesMode)viewModel.Mode;

            switch (mode)
            {
            case SeriesMode.Binary:
            {
                var data = generator.GenerateBitsAsChars(viewModel.Length);
                var r    = LfsrHelper.GetRegisterAsStringArray(generator.Registers);
                return(Json(new { data = new string(data), registers = r }));
            }

            case SeriesMode.Hex:
            {
                var data = generator.GenerateBytes(viewModel.Length);
                var r    = LfsrHelper.GetRegisterAsStringArray(generator.Registers);
                return(Json(new { data = BitConverter.ToString(data), registers = r }));
            }

            case SeriesMode.Base64:
            {
                var data = generator.GenerateBytes(viewModel.Length);
                var r    = LfsrHelper.GetRegisterAsStringArray(generator.Registers);
                return(Json(new { data = data, registers = r }));
            }
            }

            return(Json(""));
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            uint parsedValue;

            if ((series_length.Text).ToString().Length == 0)
            {
                MessageBox.Show("Podaj długość ciągu do wygenerowania.");
                return;
            }
            else if (!uint.TryParse(series_length.Text, out parsedValue))
            {
                MessageBox.Show("Długość ciągu do wygenerowania musi być dodatnią liczbą całkowitą.");
                return;
            }

            uint parsed;

            if (!uint.TryParse(lfsr1.Text, out parsed))
            {
                MessageBox.Show("Wartości początkowe rejestrów muszą być dodatnią liczbą całkowitą.");
                return;
            }
            else if ((lfsr1.Text).ToString().Length == 0)
            {
                MessageBox.Show("Uzupełnij pola z wartościami początkowymi rejestrów.");
                return;
            }

            var registersLength = Convert.ToInt32(RegisterLength_ComboBox.SelectedItem);

            int numOfLfsr = 1;

            Lfsr[] lfsr = new Lfsr[numOfLfsr];

            var boolArray = Convert.ToString(parsed, 2).Select(str => str.Equals('1')).Take(registersLength).ToArray();
            var bitArray  = new BitArray(registersLength);

            for (int j = 0; j < boolArray.Length; j++)
            {
                bitArray[j] = boolArray[j];
            }

            lfsr[0] = new Lfsr(registersLength);
            TextBox tb = (TextBox)this.FindName("lfsr1");

            lfsr[0].SetRegisterValues(bitArray);


            wynik.Clear();
            LfsrGenerator generator = new SelfDecimationGenerator(lfsr);


            if (typ.SelectedIndex == 0)
            {
                var gen = generator.GenerateBitsAsChars(Convert.ToInt32(series_length.Text));
                wynik.Text = new string(gen);
            }
            else if (typ.SelectedIndex == 1)
            {
                var gen1 = generator.GenerateBytes(Convert.ToInt32(series_length.Text));
                wynik.Text = BitConverter.ToString(gen1);
            }
            else if (typ.SelectedIndex == 2)
            {
                var gen1 = generator.GenerateIntegers(Convert.ToInt32(series_length.Text));
                wynik.Text = String.Join(" ", gen1.Select(p => p.ToString()).ToArray());
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int parsedValue;

            if (!int.TryParse(series_length.Text, out parsedValue))
            {
                MessageBox.Show("Długość ciągu do wygenerowania musi być liczbą całkowitą.");
                return;
            }
            else if ((series_length.Text).ToString().Length == 0)
            {
                MessageBox.Show("Podaj długość ciągu do wygenerowania.");
                return;
            }
            else if (Convert.ToInt32(series_length.Text) < 1)
            {
                MessageBox.Show("Długość ciągu do wygenerowania musi być większa od zera.");
                return;
            }
            byte parsed;

            if (!byte.TryParse(lfsr1.Text, out parsed))
            {
                MessageBox.Show("Musisz podać wartość początkową rejestru z zakresu (0-255).");
                return;
            }
            else if ((lfsr1.Text).ToString().Length == 0)
            {
                MessageBox.Show("Uzupełnij pole z wartością początkowom rejestru.");
                return;
            }


            int numOfLfsr = 1;

            Lfsr[] lfsr = new Lfsr[numOfLfsr];
            for (int i = 0; i < numOfLfsr; i++)
            {
                lfsr[i] = new Lfsr();
                TextBox tb = (TextBox)this.FindName("lfsr" + (i + 1).ToString());
                lfsr[i].SetRegisterValues(new BitArray(new byte[] { Convert.ToByte(tb.Text) }));
            }

            Stopwatch sw = new Stopwatch();

            wynik.Clear();
            LfsrGenerator generator = new SelfDecimationGenerator(lfsr);

            if (typ.SelectedIndex == 0)
            {
                sw.Start();
                var gen = generator.GenerateBitsAsChars(Convert.ToInt32(series_length.Text));
                sw.Stop();
                foreach (var item in gen)
                {
                    wynik.Text = wynik.Text + item;
                }
                sw.Reset();
            }
            else if (typ.SelectedIndex == 1)
            {
                sw.Start();
                var gen1 = generator.GenerateBytes(Convert.ToInt32(series_length.Text));
                sw.Stop();
                foreach (var item in gen1)
                {
                    wynik.Text = wynik.Text + item;
                }

                sw.Reset();
            }
            else if (typ.SelectedIndex == 2)
            {
                sw.Start();
                var gen1 = generator.GenerateIntegers(Convert.ToInt32(series_length.Text));
                sw.Stop();
                foreach (var item in gen1)
                {
                    wynik.Text = wynik.Text + item;
                }
                sw.Reset();
            }
        }
예제 #4
0
        private void GenerateBtn_Click(object sender, RoutedEventArgs e)
        {
            string r1 = r1State.Content as string;

            Lfsr[] lfsrs = new Lfsr[1];

            lfsrs[0] = new Lfsr(r1);

            //setting custom feedback function
            ChangeLfsrFeedbackFunctions(lfsrs);

            for (int i = 0; i < lfsrs.Length; i++)
            {
                generator.ChangeRegister(lfsrs[i], i);
            }

            int dValue = 0;
            int kValue = 0;

            dValue            = int.Parse(D_value.Text);
            kValue            = int.Parse(K_value.Text);
            generator.D_value = dValue;
            generator.K_value = kValue;

            var format = outputFormatComboBox.SelectedIndex;

            int seriesLength = 0;

            if (outputLength.Text != "")
            {
                seriesLength = int.Parse(outputLength.Text);
            }

            ClearOutput();

            Task.Run(() =>
            {
                SetLoadingCircle(true);
                disableButtons();
                ResetFipsIcons();
                byte[] generatedBytes = null;
                switch (format)
                {
                case 0:
                    {
                        lastGeneratedFormat = format;
                        try
                        {
                            lastGeneratedString = new string(generator.GenerateBitsAsChars(seriesLength));
                        }
                        catch (Exception ex)
                        {
                            SetLoadingCircle(false);
                            MessageBox.Show(ex.Message);
                            return;
                        }

                        SetOutputText(lastGeneratedString);
                        SetLoadingCircle(false);
                        break;
                    }

                case 1:
                    {
                        try
                        {
                            generatedBytes = generator.GenerateBytes(seriesLength);
                        }
                        catch (Exception ex)
                        {
                            SetLoadingCircle(false);
                            MessageBox.Show(ex.Message);
                            return;
                        }
                        lastGeneratedFormat = format;
                        lastGeneratedString = Convert.ToBase64String(generatedBytes);
                        SetOutputText(lastGeneratedString);
                        SetLoadingCircle(false);
                        break;
                    }

                default:
                    break;
                }
                UpdateRegisterState(generator);
                enableButtons();

                if (seriesLength >= 20000)
                {
                    SingleBitTestResult singleBitResult   = null;
                    SeriesTestResult seriesResult         = null;
                    LongSeriesTestResult longSeriesResult = null;
                    PokerTestResult pokerResult           = null;

                    if (format == 0)
                    {
                        FipsTests fipsTests = new FipsTests();
                        var testInput       = lastGeneratedString.Substring(0, 20000);

                        singleBitResult  = fipsTests.SingleBitTest(testInput);
                        seriesResult     = fipsTests.SeriesTest(testInput);
                        longSeriesResult = fipsTests.LongSeriesTests(testInput);
                        pokerResult      = fipsTests.PokerTest(testInput);
                    }
                    else if (format == 1)
                    {
                        FipsTests fipsTests = new FipsTests();
                        byte[] testArray    = new byte[2500];
                        Array.Copy(generatedBytes, testArray, 2500);
                        if (generatedBytes != null)
                        {
                            singleBitResult  = fipsTests.SingleBitTest(testArray);
                            seriesResult     = fipsTests.SeriesTest(testArray);
                            longSeriesResult = fipsTests.LongSeriesTests(testArray);
                            pokerResult      = fipsTests.PokerTest(testArray);
                        }
                    }

                    if (singleBitResult.TestPassed)
                    {
                        singleBitSuccess();
                    }
                    else
                    {
                        singleBitFail();
                    }
                    if (seriesResult.TestPassed)
                    {
                        seriesSuccess();
                    }
                    else
                    {
                        seriesFail();
                    }
                    if (longSeriesResult.TestPassed)
                    {
                        longSeriesSuccess();
                    }
                    else
                    {
                        longSeriesFail();
                    }
                    if (pokerResult.TestPassed)
                    {
                        pokerSuccess();
                    }
                    else
                    {
                        pokerFail();
                    }
                }
            });
        }