快速伪随机数发生器在C [英] Fast pseudorandom number generator for cryptography in C

查看:203
本文介绍了快速伪随机数发生器在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码生成用于加密目的的伪随机数序列,但后来我在某处读到它可能不是很安全。有人可以给我C实现一个更好的发电机 - 主要目标是这种方法是快。例如,我做了一些研究,遇到了 Blum Blum Shub 方法,这将通过做pow(N)来完全杀死性能,计算。

I was using the following code to generate sequence of pseudo-random numbers that was used for cryptographic purposes, but then I read somewhere that it may not be very secure. Can someone give me C implementation of a better generator -- the main goal is for this method to be fast. For instance, I did some research and came across Blum Blum Shub method, which would totally kill performance by doing pow(N) calculations.

PS。请不要引用维基百科的文章w / o C / C ++代码。我正在寻找下面显示的C或C ++代码示例。

PS. And please don't quote Wikipedia articles w/o C/C++ code. I'm looking for C or C++ code sample of what I'm showing below.

#define ROL(v, shift) ((((v) >> ((sizeof(v) * 8) - (shift))) | ((v) << (shift))))

ULONGLONG uiPSN = doSeed();   //64-bit unsigned integer

for(int i = 0; i < sizeOfArray; i++)
{
    uiPSN = uiPSN * 214013L + 2531011L;
    uiPSN = ROL(uiPSN, 16);

    //Apply 'uiPSN'
}


推荐答案

ISAAC( http://www.burtleburtle.net/bob/rand/isaacafa。 html )可能是最快的加密安全PRNG(代码在现场)之一。另一种方法是在计数器模式中使用块密码。像TwoFish这样相当快速和免费的,将是有效的。

ISAAC (http://www.burtleburtle.net/bob/rand/isaacafa.html) is probably one of the fastest cryptographically secure PRNGs (code at site). Another approach is to use a block cipher in counter mode. Something like TwoFish, which is reasonably fast and freely available, would be effective.

如果你不需要很多数字,所有现代操作系统都内置RNG适合于加密使用,尽管它们通常不能产生大量的数字,因为它们依赖于从诸如输入定时的源累积熵。类Unix系统(Linux,OSX)有/ dev / random,Windows有CryptGenRandom。即使这些不适合你的需要,你也许应该使用它们种子你最终使用的PRNG。

If you don't need a lot of numbers, all modern operating systems have built-in RNGs suitable for cryptographic use, though they typically can't produce lots of numbers because they rely on accumulating entropy from sources like input timings. Unix-like systems (Linux, OSX) have /dev/random, Windows has CryptGenRandom. Even if these aren't suitable for your needs, you probably should use them to seed the PRNG you do end up using.

这篇关于快速伪随机数发生器在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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