English 中文(简体)
Srand让我的计划冻结了
原标题:srand is causing my program to freeze

我已经在 C+++ 中实施了 RSA 算法, 程序正在运行中, 然而随机调用正在使程序慢化。 我用 Srand 来生成两个质数和加密密钥( e) 。 这里的片段

...............................................
do
{
    p = make_prime_number();
    q = make_prime_number();
}while(p == q);

phi = (p - 1) * (q - 1);
n = p * q;

do
{
    e = random_builder (20);

    int *t = extended_gcd (e, phi);
    d = t[0];
}while(gcd(e, phi) != 1 || d < 1 || d >= n );
...............................................

int random_builder(const int max)
{
    srand(time(NULL));

    return rand() % max + 1;
}

bool is_prime(const int num)
{
    for(int i = 2; i <= num/2; i++)
        if(num % i == 0) 
            return false;

    return true;
}

int make_prime_number()
{
    int num;

    do
    {
        num = random_builder(20);
    }while(not is_prime(num));

    return num;
}

我们能不能通过改变草原的种子来加快这个过程?

最佳回答

无需多次调用 srand () 。 请在程序启动时调用 once , 然后不要管它 。

在调用具有特定种子价值的 srand () () < code > () < em> same 随机数字序列由 rand () () > () () < amsame ) < orgoted () ()) 生成。因此,既然您现在在 < em> seconds 中调用,您的 < code> random_ buildinger () 函数只在每次 < 坚固 > 秒

问题回答

您根本不应该使用 rand () srand 。 您应该使用种子精密的加密PRNG。 在 Linux 上,您可以简单地阅读 / dev/urandom 和窗口上的 < code>CryptGenRandom 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签