how to use gmp random numbers and how to update the numbers witch click-button?

Viewed 18

I want to generate new random numbers when i press the "generate key" button, but i get always the same numbers. I initialised the state, the mpz_t integers, etc. but i probably don't know how to use it correctly. pls help.

        private void BtnCreateKey(object sender, RoutedEventArgs e)
    {
        mpz_t modulo = new mpz_t();
        mpz_t basis = new mpz_t();
        mpz_t alicePrivate = new mpz_t();
        mpz_t bobPrivate = new mpz_t();
        mpz_t privateKeyAlice = new mpz_t();
        mpz_t privateKeyBob = new mpz_t();
        mpz_t sharedSecretKeyAlice = new mpz_t();
        mpz_t sharedSecretKeyBob = new mpz_t();
        mpz_t exchangeKeyAlice = new mpz_t();
        mpz_t exchangeKeyBob = new mpz_t();

        gmp_randstate_t rnd = new gmp_randstate_t();
        gmp_lib.gmp_randinit_mt(rnd);

        gmp_lib.gmp_randseed_ui(rnd, 100000U);

        //init
        gmp_lib.mpz_init(alicePrivate);
        gmp_lib.mpz_init(bobPrivate);
        gmp_lib.mpz_init(modulo);
        gmp_lib.mpz_init(basis);
        gmp_lib.mpz_init(privateKeyAlice);
        gmp_lib.mpz_init(privateKeyBob);
        gmp_lib.mpz_init(exchangeKeyAlice);
        gmp_lib.mpz_init(exchangeKeyBob);
        gmp_lib.mpz_init(sharedSecretKeyAlice);
        gmp_lib.mpz_init(sharedSecretKeyBob);

        gmp_lib.mpz_urandomb(modulo, rnd, 32);
        gmp_lib.mpz_urandomb(basis, rnd, 8);

        gmp_lib.mpz_urandomb(alicePrivate, rnd, 16);
        gmp_lib.mpz_urandomb(bobPrivate, rnd, 16);
1 Answers

Fix: the problem was that seed and state were initialized every time the button was clicked, so they generated the same number from the same seed over and over again. Seed and state need to be init once (f.e. in the constructor) and cleared in the destructor. And for the better seed i used the actual time.

    gmp_randstate_t rnd = new gmp_randstate_t();

    public MainWindow()
    {
        InitializeComponent();
        gmp_lib.gmp_randinit_mt(rnd);
        gmp_lib.gmp_randseed_ui(rnd, (uint)DateTime.UtcNow.Second);
    }
    ~MainWindow()
    {
        gmp_lib.gmp_randclear(rnd);
    }
Related