Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the paper An Efficient Certificateless Encryption for Secure
Data Sharing in Public Clouds
section 2.3 says:

KGC takes as input a security parameter k to generate two primes p and q such that q|p − 1.

I do it as generate 2 prime p and q, ok if they are not the same, and if (p - 1) % q !=0 it will regenerate another 2 prime numbers to check. So I have tried to generate it with this code.
C#
protected void btnstart_Click(object sender, EventArgs e)
        {
            generatepnq();     
        }

        public void generatepnq()
        {
            BigInteger p = GenPrime();
            BigInteger q = GenPrime();
            do
            {
                q = GenPrime();
            } while (p == q);
            
            var result = CheckIfDivisible(p, q);
            do
            {
                generatepnq();
            } while (result == false);

            txtP.Text = p.ToString();
            txtQ.Text = q.ToString();        
        }

        public BigInteger GenPrime()
        {
            Random rand = new Random();
            BigInteger primenumber = BigInteger.genPseudoPrime(64, 5, rand);
            return primenumber;
        }

        public bool CheckIfDivisible(BigInteger p, BigInteger q)
        {
            if ((p - 1) % q == 0)
                return true;
            else
                return false;
        }     


But it takes a lot of computational time which leads to infinite loop. May I know of a better way to rewrite the code? Thanks!

What I have tried:

rewriting the code, and rearrange, but it still seems to do the infinite loop, need a better solution.
Posted

1 solution

Um.
Look at your code:
C#
var result = CheckIfDivisible(p, q);
do
{
    generatepnq();
} while (result == false);
Since result is a local variable and you never send it to the method, once it enters the loop, it will never exit. To make matters worse, that code is inside the generatepnq method, so it will eventually run out of stack while trying to recursively call itself an infinite number of times...

Please, use the debugger - look at exactly what is going on, because it would have taken you seconds to find out that the infinite loop is not where you think it is!
 
Share this answer
 
Comments
HitsugayaHisagi 13-Feb-16 12:54pm    
I did use the debugger and see how it goes though. It did enter into the CheckIfDivisible method, together with both the generated prime numbers. But because it did not fulfill the condition so it return false and the whole process repeated till like an infinite loop. That's what I understand from how the code moves.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900