Esempio n. 1
0
        public IActionResult GeffeGenerate([FromBody] LfsrGeneratorsViewModel viewModel)
        {
            var numOfRegisters = viewModel.Registers.Length;

            Lfsr[] registers = new Lfsr[3];

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

            GeffesGenerator generator = new GeffesGenerator(registers);

            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 = new uint[3];
            if ((lfsr1.Text).ToString().Length == 0 || (lfsr2.Text).ToString().Length == 0 || (lfsr3.Text).ToString().Length == 0)
            {
                MessageBox.Show("Uzupełnij pola z wartościami początkowymi rejestrów.");
                return;
            }
            else if (!uint.TryParse(lfsr1.Text, out parsed[0]) || !uint.TryParse(lfsr3.Text, out parsed[1]) || !uint.TryParse(lfsr2.Text, out parsed[2]))
            {
                MessageBox.Show("Wartości początkowe rejestrów muszą być dodatnią liczbą całkowitą.");
                return;
            }



            int numOfLfsr = 3;

            Lfsr[] lfsr = new Lfsr[numOfLfsr];

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


            for (int i = 0; i < numOfLfsr; i++)
            {
                lfsr[i] = new Lfsr(registersLength);
                TextBox tb = (TextBox)this.FindName("lfsr" + (i + 1).ToString());

                var boolArray = Convert.ToString(parsed[i], 2).Select(s => s.Equals('1')).Take(registersLength).ToArray();
                var bitArray  = new BitArray(registersLength);
                for (int j = 0; j < boolArray.Length; j++)
                {
                    bitArray[j] = boolArray[j];
                }

                lfsr[i].SetRegisterValues(bitArray);
            }

            wynik.Clear();
            LfsrGenerator generator = new GeffesGenerator(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 GenerateBtn_Click(object sender, RoutedEventArgs e)
        {
            string r1 = r1State.Content as string;
            string r2 = r2State.Content as string;
            string r3 = r3State.Content as string;

            Lfsr[] lfsrs = new Lfsr[3];

            lfsrs[0] = new Lfsr(r1);
            lfsrs[1] = new Lfsr(r2);
            lfsrs[2] = new Lfsr(r3);


            //setting custom feedback function
            ChangeLfsrFeedbackFunctions(lfsrs);

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

            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;
                        lastGeneratedString = new string(generator.GenerateBitsAsChars(seriesLength));
                        SetOutputText(lastGeneratedString);
                        SetLoadingCircle(false);
                        break;
                    }

                case 1:
                    {
                        generatedBytes      = generator.GenerateBytes(seriesLength);
                        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();
                    }
                }
            });
        }
Esempio n. 4
0
        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) || !byte.TryParse(lfsr3.Text, out parsed) || !byte.TryParse(lfsr2.Text, out parsed))
            {
                MessageBox.Show("Musisz podać wartości początkowe rejestrów z zakresu (0-255).");
                return;
            }
            else if ((lfsr1.Text).ToString().Length == 0 || (lfsr2.Text).ToString().Length == 0 || (lfsr3.Text).ToString().Length == 0)
            {
                MessageBox.Show("Uzupełnij pola z wartościami początkowymi rejestrów.");
                return;
            }


            int numOfLfsr = 3;

            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 GeffesGenerator(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();
            }
        }