真实随机数如何js 产生随机数?为什么说真实随机数可以抵抗恶意攻击

什么是伪随机数?
  &1.伪随机数是看似随机实质是固定的周期性序列,也就是有规则的随机。
  2.只要这个随机数是由确定算法生成的,那就是伪随机,只能通过不断算法优化,使你的随机数更接近随机。
  &  (随机这个属性和算法本身就是矛盾的)
  3.通过真实随机事件取得的随机数才是真随机数。
Java随机数产生原理:
  &Java的随机数产生是通过线性同余公式产生的,也就是说通过一个复杂的算法生成的。伪随机数的不安全性:
& & & Java自带的随机数函数是很容易被黑客破解的,因为黑客可以通过获取一定长度的随机数序列来推出你的seed,然后就可以预测下一个随机数。
不用种子的不随机性会增大的原因:
& & & & & java.Math.Random()实际是在内部调用java.util.Random()的,使用一个和当前系统时间有关的数字作为种子数。两个随机数就很可能相同。
& & & & & & &double a = Math.random();
& & & & & & &double b = Math.random();
& & & & & & & Random r1 = new Random();
     &&r1.nextInt(10);
& & & & & & & Random r2 = new Random();
     r2.nextInt(10);
Java中产生随机数的方法有两种:
  第一种:Math.random()
  第二种:new Random()
一、java.lang.Math.Random:
  调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0),在该范围内(近似)均匀分布。因为返回值是double类型的,小数点后面可以保留15位小数,所以产生相同的可能性非常小,在这一定程度上是随机数。
二、java.util.Random:& & & Random r1 = new Random();& & & Random r2 = new Random();
& & & Random r3 = new Random(10);& & & Random r4 = new Random(10);
下面Random()的两种构造方法:
  & Random():使用一个和当前系统时间对应的相对时间有关的数字作为种子数。
  &&Random(long seed):直接传入一个种子数。
种子的作用是什么?
  种子就是产生随机数的第一次使用值,机制是通过一个函数,将这个种子的值转化为随机数空间中的某一个点上,并且产生的随机数均匀的散布在空间中。以后产生的随机数都与前一个随机数有关。
举例:  Random r =new Random(100);  System.out.println(r.nextInt(20));
  种子数只是随机算法的起源数字,和生成的随机数字的区间没有任何关系。
初始化时100并没有起直接作用(注意:不是没有起作用),r.nextInt(20)中的20是随机数的上限,产生的随机数为0-20的整数,不包括20。
Random r1 = new Random();
Random r2 = new Random();
//无参构造使用的是参数作为种子数
Random r3 = new Random(100);
Random r4 = new Random(100);
//产生随机数调用nextXXX()方法
System.out.println(r1.nextInt(10));
System.out.println(r1.nextInt(10));
System.out.println(r2.nextInt(10));
System.out.println(r2.nextInt(10));
System.out.println("-----------------");
System.out.println(r3.nextInt(10));
System.out.println(r3.nextInt(10));
System.out.println(r4.nextInt(10));
System.out.println(r4.nextInt(10));
-----------------
总结:  1.同一个种子,生成N个随机数,当你设定种子的时候,这N个随机数是什么已经确定。相同次数生成的随机数字是完全相同的。
  2.如果用相同的种子创建两个 Random 实例,如上面的r3,r4,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
& & & 3.Java的随机数都是通过算法实现的,Math.random()本质上属于Random()类。
& & & 4.使用java.util.Random()会相对来说比较灵活一些。
阅读(...) 评论()The question posed came about during a 2nd Year Comp Science lecture while discussing the impossibility of generating numbers in a deterministic computational device.
This was the only suggestion which didn't depend on non-commodity-class hardware.
Subsequently nobody would put their reputation on the line to argue definitively for or against it.
Anyone care to make a stand for or against.
If so, how about a mention as to a possible implementation?
解决方案 I'll put my rep on the line (at least, 2 points of it per downvote).
A malicious machine on your network could use ARP spoofing (or a number of other techniques) to intercept your pings and reply to them after certain periods. They would then not only know what your random numbers are, they would control them.
Of course there's still the question of how deterministic your local network is, so it might not be as easy as all that in practice. But since you get no benefit from pinging random IPs on the internet, you might just as well draw entropy from ethernet traffic.
Drawing entropy from devices attached to your machine is a well-studied principle, and the pros and cons of various kinds of devices and methods of measuring can be e.g. stolen from the implementation of /dev/random.
[Edit: as a general principle, when working in the fundamentals of security (and the only practical needs for significant quantities of truly random data are security-related) you MUST assume that a fantastically well-resourced, determined attacker will do everything in their power to break your system.
For practical security, you can assume that nobody wants your PGP key that badly, and settle for a trade-off of security against cost. But when inventing algorithms and techniques, you need to give them the strongest security guarantees that they could ever possibly face. Since I can believe that someone, somewhere, might want someone else's private key badly enough do build this bit of kit to defeat your proposal, I can't accept it as an advance over current best practice. AFAIK /dev/random follows fairly close to best practice for generating truly random data on a cheap home PC]
[Another edit: it has suggested in comments that (1) it is true of any TRNG that the physical process could be influenced, and (2) that security concerns don't apply here anyway.
The answer to (1) is that it's possible on any realistic hardware to do so much better than ping response times, and gather more entropy faster, that this proposal is a non-solution. In CS terms, obviously you can't generate random numbers on a deterministic machine, which is what provoked the question. But then in CS terms a machine with any external input stream is non-deterministic by definition, so if we're talking about ping then we aren't talking about deterministic machines. So it makes sense to look at the real inputs that real machines have, and consider them as sources of randomness. No matter what your machine, raw ping times are not high on the list of sources available, so they can be ruled out before worrying about good the better ones are. Assuming that a network is not subverted is a much bigger (and unnecessary) assumption than assuming that your own hardware is not subverted.
The answer to (2) is philosophical. If you don't mind your random numbers having the property that they can be chosen at whim instead of by chance, then this proposal is OK. But that's not what I understand by the term 'random'. Just because something is inconsistent doesn't mean it's necessarily random.
Finally, to address the implementation details of the proposal as requested: assuming you accept ping times as random, you still can't use the unprocessed ping times as RNG output. You don't know their probability distribution, and they certainly aren't uniformly distributed (which is normally what people want from an RNG).
So, you need to decide how many bits of entropy per ping you are willing to rely on. Entropy is a precisely-defined mathematical property of a random variable which can reasonably be considered a measure of how 'random' it actually is. In practice, you find a lower bound you're happy with. Then hash together a number of inputs, and convert that into a number of bits of output less than or equal to the total relied-upon entropy of the inputs. 'Total' doesn't necessarily mean sum: if the inputs are statistically independent then it is the sum, but this is unlikely to be the case for pings, so part of your entropy estimate will be to account for correlation. The sophisticated big sister of this hashing operation is called an 'entropy collector', and all good OSes have one.
If you're using the data to seed a PRNG, though, and the PRNG can use arbitrarily large seed input, then you don't have to hash because it will do that for you. You still have to estimate entropy if you want to know how 'random' your seed value was - you can use the best PRNG in the world, but its entropy is still limited by the entropy of the seed.]
本文地址: &
在第2年比较科学的讲座提出的问题是约在讨论中确定的计算设备产生的数字是不可能的。
这是它并不取决于非商品类硬件的唯一的建议。
随后没有人会放就行了他们的声誉,以明确主张或反对。
任何人都照顾到了,为捍卫或反对。如果是这样,如何一提,以一种可能的实现?
解决方案 我会把我代表就行了(至少2点它每downvote)。
在网络上恶意的机器可以使用ARP欺骗(或其他一些技术)拦截您的ping和某些时期后回复他们。然后,他们不仅知道你的随机数,他们将控制他们。
当然,还是有你的本地网络是如何确定的问题,所以它可能不会像所有的,在实践中容易。但因为你从互联网上随机查验IP地址没有得到好处,你还不如从以太网流量得出的熵。
从绘图连接到您的机器设备熵充分研究的原则,并测量的优点和各种设备的缺点和方法可以是如从/ dev目录下的实现被偷/随机的。
[修改:作为一般原则,在安全的基础工作(和显著量的真正的随机数据的唯一现实的需求是与安全相关的),当你必须假设一个极其良好-resourced,确定攻击者会尽一切力量来破坏你的系统。
有关实际的安全性,可以认为没有人希望你的PGP密钥时失态,满足于对成本进行权衡安全性。但是发明的算法和技术时,你需要给他们最强大的安全保障,他们可能永远可能面对的问题。因为我相信,有人在什么地方,可能需要别人的私钥严重不足做构建套件此位,打败你的建议,我不能接受它作为一个提前结束当前的最佳实践。 AFAIK的/ dev /随机如下相当接近最佳实践产生廉价的家用电脑真正的随机数据]
[另一个修改:它曾建议在评论认为,(1)它是任何真随机数发生器的真实的物理过程可能受到影响,和(2)的安全问题并不适用于这里反正
这个问题的答案(1),它可能在任何实际的硬件做这么多比ping响应时间,并收集更多的熵快,这个建议是一个无解。在CS方面,显然你不能生成一个确定性的机器,这是引发问题的随机数。但随后在CS方面的机器与任何外部输入流具有不确定性的定义,所以,如果我们谈论的平话,我们现在谈的不是确定性的机器。因此,它是有道理的看,真正的机器有真正的投入,并考虑他们作为随机的来源。不管你是什么机器,原料平次不高的可用来源的名单上,这样他们就可以不必担心好的更好的是之前被排除。假设没有颠覆网络是一个比假设你自己的硬件不会颠覆了更大的(和不必要的)的假设。
的答案(2)是哲学。如果你不介意你的随机数有,他们可以随心所欲,而不是一个偶然的机会被选择的属性,那么这个提议确定。但是,这不是我所理解的术语“随机”。仅仅因为一些不一致并不意味着它一定是随机的。
最后,为解决方案的实施细则的要求:假设你接受ping时间为随机的,你仍然不能使用未经处理的ping时间为RNG输出。你不知道自己的概率分布,他们肯定不是均匀分布的(通常是人们想要什么,从RNG)。
所以,你需要决定你有多少每坪熵位愿意依靠。熵是可以合理地认为它是如何“随机”是衡量一个随机变量的precisely定义的数学特性。在实际应用中,你会发现一个下界你满意。然后散列一起多个输入,并且将其转换成数字输出的比特小于或等于依赖-时的输入熵的总和。 “总计”并不一定意味着总和:如果输入是统计独立的,则它是和,但是这是不太可能的坪的情况下,这样的熵估算的部分将是占相关性。这个哈希运算的复杂的大姐姐被称为“熵收藏家”,并且所有美好的操作系统有一个。
如果你使用的数据种子一个PRNG,虽然和PRNG可以使用任意大的种子投入,那么你不必散列,因为它会为你做的。你还是要估计的熵,如果你想知道如何“随机”的种子值是 - 你可以用最好的PRNG世界,但它的熵是由种子的熵仍然有限]
本文地址: &
扫一扫关注官方微信以下试题来自:
单项选择题数字签名和随机数挑战不能防范以下哪种攻击或恶意行为?()。
A、伪装欺骗
B、重放攻击
D、DOS攻击
为您推荐的考试题库
您可能感兴趣的试卷
你可能感兴趣的试题
B、访问控制
A、碰撞约束
B、入侵检测
C、组合散列
D、随机数生成器
A、可能有很大的变化
B、一定有很大的变化
C、可能有很小的变化
D、一定有很小的变化
A、密钥太短,已经能被现代计算机暴力破解
B、加密算法有漏洞,在数学上已被破解
C、留有后门,可能泄露部分信息
D、算法过于陈旧,已经有更好的替代方案
A、确定加密算法的强度
B、增加加密算法的代替功能
C、减少加密算法的换为功能
D、确定所使用的换位如何在各种编程语言中生成安全的随机数? -
| 关注黑客与极客
如何在各种编程语言中生成安全的随机数?
共268318人围观
,发现 12 个不明物体
生成安全的随机数据指什么?为什么要生成安全的随机数据?之前一些文献中这并没有很好得说明如何生成“安全”的随机数。所以,这里将介绍如何在下面的编程语言中安全地生成随机数。
&&& C/C++&&& Java&&& .NET&&& Node.js&&& PHP&&& Python&&& Ruby
需要包含的一般条件
这篇文章的所有方案都必须只从内核的CSPRNG(Cryptographically Secure Pseudo-Random Number Generator,密码安全的伪随机数生成器)中读取,并且失败后立即关闭。用户空间的RNG以及回退到不安全的RNG都是不允许的。所以,根据平台的不同,使用下面的熵源:
&&& Windows:
&&&&&&& RtlGenRandom
&&& Linux:
&&&&&&& getrandom (如何可用的话)
&&&&&&&&&&& 它的方法是正确的,在播种之前会阻塞,之后不再播种。
&&&&&&& /dev/urandom (老的Linux内核)
&&&&&&&&&&& 对于在Linux启动时运行的软件,查询/dev/random,直到它可用。这意味着那时/dev/urandom已经播种了,你可以安全地从/dev/urandom中读取内容了,可以用到你的密码中。不要从/dev/random中读取。
&&& OpenBSD:
&&&&&&& getentropy()
&&&&&&& arc4random_buf() 使用ChaCha20加密算法 (不是RC4)
&&& 其它类Unix系统 (包括OS X):
&&&&&&& /dev/urandom
这里不考虑依赖于haveged,egd等程序的解决方案。
C/C++中的密码安全随机
最简单和安全的方法是,把libsodium库添加到工程的依赖库中,使用randombytes_buf()函数。
在查看libsodium是怎样实现这些函数的。PHP团队在其内部的random_bytes函数实现中采用了与此类似的方法。
#include "sodium.h"int foo() {&&& char myString[32];&&& int myI&& &&&& randombytes_buf(myString, 32);&&& /* myString will be a string of 32 random bytes */&&& myInt = randombytes_uniform(10);&&& /* myInt will be a random number between 0 and 9 */}
如果可以的话就使用libsodium,下面的其它语言也是如此。
Java中的密码安全随机
除了使用libsodium(推荐),也可以直接使用Java的SecureRandom类:
SecureRandom csprng = new SecureRandom();byte[] randomBytes = new byte[32];csprng.nextBytes(randombytes);
注意:不要在Linux上使用SecureRandom.getInstanceStrong(),不要被名称误导,它等同于读取/dev/random,这个是不安全的。Java8中new SecureRandom()默认读取/dev/urandom,这才是你需要的。
.NET(C#)中的密码安全随机
普遍采用的方案是使用System.Security.Cryptography.RNGCryptoServiceProvider,比如:
RandomNumberGenerator csprng = new RNGCryptoServiceProvider();byte[] rawByteArray = new byte[32];csprng.getBytes(rawByteArray);
如果你需要生成密码上安全的整数,查看Inferno(一个Stan Drapkin写的.NET密码库)中的CryptoRandom类的实现方法。
Node.js中的密码安全随机
不要使用crypto.randomBytes()
&在openssl抹掉crypto.randomBytes之前,你最好的选择是使用node-sodium(来替代这个函数)&
var csprng = require("sodium").Rvar bytes = csprng.randombytes_buf(32);
PHP中的密码安全随机
如果你运行的是PHP 7,有一个内置的函数:
$string = random_bytes(32);$integer = random_int(0, PHP_INT_MAX);
如何你用的还是PHP 5, 获取random_compat,然后同PHP 7一样使用相同的API。
composer require paragonie/random_compat:^2
请使用版本2。版本1会回退到OpenSSL,如果没有其它可用的熵源,它会导致安全问题。然而,一些人为了兼容性,会明确地使用版本1。
如果你在写一个供别人在他们的工程中使用的PHP 5库,将你的composer.json条件字符串设置为^1|^2。相反,如果你在写一个应用程序,将条件字符串设置为^2。
Python中的密码安全随机
如果你没有使用libsodium:
如果你需要随机字节,使用os.urandom().
如果你需要其它格式的随机数据,你需要使用random.SystemRandom(),而不是random。
import sysimport random# Random bytesbytes = os.urandom(32)csprng = random.SystemRandom()# Random (probably large) integerint = csprng.randint(0, sys.maxint)
Ruby中的密码安全随机
不要使用Ruby的SecureRandom!
与名称无关,它不是最好的CSPRNG。幸运的是,Tony Arcieri(密码专家, 的设计者,全面的密码应用工程师)给Ruby community提供了一个安全的选择,将libsodium的sysrandom接口移植到了Ruby gem中。
建议:使用Sysrandom代理SecureRandom。
安装sysrandom:
gem install sysrandom
Sysrandom与SecureRandom API兼容。可以通过打补丁来代替SecureRandom。
*本文译者:felix,翻译自:,转载须注明来自FreeBuf黑客与极客()
c4td0g, 信安从业者,信安爱好者。(各位爷,轻点喷)
PHP是世界上最好的语言!Python是国内最好的语言! :mrgreen:
若可以联网的话取random.org的api可以么
PHP是世界上最好的语言!
/dev/random
你需要一台量子密钥生成器[噢耶]
必须您当前尚未登录。
必须(保密)
这家伙太懒,还未填写个人描述!
关注我们 分享每日精选文章
blackscreen}

我要回帖

更多关于 产生随机数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信