【问题标题】:How to supply seed to ISAAC random number generator如何向 ISAAC 随机数生成器提供种子
【发布时间】:2016-04-03 05:44:31
【问题描述】:

我有以下代码使用 ISAAC 生成随机数:

int main()
{
    /* Initialize the structure to 0 */
    randctx ctx;
    ctx.randa = ctx.randb = ctx.randc = (ub4)0;

    /* Initialize the seed */
    for (ub4 i=0; i<256; ++i) {
        ctx.randrsl[i] = i;
    }

    /* Initialize the random numbers from the seed */
    randinit(&ctx, TRUE);

    printf("%.8lx\n", rand(&ctx));
}

上面的代码是从How to use ISAAC in C得到的

我还需要从 /dev/random 读取代码以获取种子:

int myFile = open("/dev/random", O_RDONLY);            
uint32_t rand;            
uint32_t randomNum = read(myFile, &rand, sizeof(rand)) ;
printf(" %u \n", rand);
close(myFile);

如何向 ISAAC 提供种子(即 rand)?

谢谢

【问题讨论】:

    标签: c random cryptography


    【解决方案1】:

    当将TRUE 传递给randinit 的标志时,种子取自ctx.randrsl。但我建议使用 RANDSIZsizeof(ctx.randrsl) 而不是硬编码其值 256。

    所以:

    ssize_t bytes_read = read(myFile, &ctx.randrsl, sizeof(ctx.randrsl));
    if(bytes_read != sizeof(ctx.randrsl)){ printf("cannot initialize seed\n"); exit(1); }
    

    【讨论】:

    • 非常感谢您的回复!我总是无法初始化种子。看来 bytes_read 等于 sizeof(ctx.randrsl) 的概率极低。有什么办法可以解决这个问题吗?
    • @user3266083 它只是从文件中读取字节,这只会在异常情况下失败。 myFile是否打开成功(应该>= 0)?
    • 是文件打开成功。仅供参考,我在 Linux VM 上运行它。
    • @user3266083 你的机器可能没有足够的熵并且没有硬件随机发生器。尝试使用/dev/urandom(这会退回到伪随机并且不等待噪声收集)
    • 是的 urandom 工作。我猜虚拟机无法创建足够的熵。非常感谢你的帮助! :)
    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2013-08-28
    • 2021-05-15
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多