encryption : RSA algorithm

前端 未结 4 1944
你的背包
你的背包 2021-01-02 16:50

I am implementing the RSA algorithm for encryption and decryption as given here:

http://www.di-mgt.com.au/rsa_alg.html

But could not understand the random pr

4条回答
  •  不知归路
    2021-01-02 17:35

    Following the practical notes at the end of the linked page you would arrive at something like this for the prime generation:

    unsigned int get_prime(int e)
    {
        while (true)
        {
            unsigned int suspect = 1 + (unsigned int)(65535.0 * rand() / (RAND_MAX + 1.0));
            suspect &= 0x0000FFFF; // make sure only the lower 16bit are set
            suspect |= 0xC001; // set the two highest and the lowest bit
            while (!test_prime(suspect))
            {
                suspect += 2;
            }
            if (suspect < 65536 && gcd(e, suspect - 1) == 1)
                return suspect;
        }
    }
    

    test_prime is supposed to be an implementation of the Miller-Rabin test. The function above makes certain assumptions and has some drawbacks:

    • int is 32 bit
    • RAND_MAX is larger than 65536
    • rand() is usually not a good random number generator to use for serious encryption
    • The generated primes are 16bit so obviously not large enough for serious encryption anyway

    Don't use this in any production code.

    According to the article it seems ok to choose e fixed.

提交回复
热议问题