如何用条件生成素数 [英] How to generate prime number with condition

查看:118
本文介绍了如何用条件生成素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在论文安全的高效无证书加密中/>
公共云中的数据共享
第2.3节说:



KGC将安全参数k作为输入生成两个素数p和q使得q | p - 1。



我这样做是为了生成2个素数p和q,如果它们不相同,那就好了,如果(p - 1)%q!= 0,它将重新生成另外2个素数以进行检查。所以我试图用这段代码生成它。

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.

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.

推荐答案

嗯。

看看你的代码:

Um.
Look at your code:
var result = CheckIfDivisible(p, q);
do
{
    generatepnq();
} while (result == false);

result 是一个局部变量,你永远不会将它发送到方法,一旦进入循环,它将永远不会退出。更糟糕的是,该代码位于generatepnq方法内部,因此在尝试以无限次数递归调用自身时最终会耗尽堆栈...



请使用调试器 - 看看究竟发生了什么,因为它会花费你几秒钟来发现无限循环不在你认为的位置!

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!


这篇关于如何用条件生成素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆