简体   繁体   English

为什么 random.random() 在 Python 中不安全?

[英]Why is random.random() not secure in Python?

I came across this question on Stack overflow: How to randomly selection item from a list in Python and they mentioned that it is not suitable for cryptographic/security purposes.我在堆栈溢出上遇到了这个问题: 如何从 Python 中的列表中随机选择项目,他们提到它不适合加密/安全目的。

So, I found this page in the official documentation: random - Generate pseudorandom numbers所以,我在官方文档中找到了这个页面: random - Generate pseudorandom numbers

It mentions that they use a Mersenne twister to generate random numbers.它提到他们使用梅森扭曲器来生成随机数。

Isn't Mersenne twister supposed to be a pretty decent random generator (at least that's what I was told in class)? Mersenne twiner 不应该是一个相当不错的随机生成器(至少这是我在课堂上被告知的)? So why can you not use it for security purposes?那么为什么不能将它用于安全目的呢?

Mersenne twister does a decent job of mimicking statistical properties(*) of randomness, but it is a deterministic algorithm. Mersenne twiner 在模拟随机性的统计属性 (*) 方面做得不错,但它是一种确定性算法。 If two copies are set to the same state, they will produce identical results in synchronization.如果两个副本设置为相同的状态,它们将在同步中产生相同的结果。 That means fhat for crypto/security applications your security is shot if an attacker can determine your initial state.这意味着对于加密/安全应用程序,如果攻击者可以确定您的初始状态,您的安全就会被击中。 I've read that for MT this can be done by knowledgeable people after six hundred and some sequential observations.我读过,对于 MT,这可以由知识渊博的人在六百次连续观察后完成。

Bottom line - use it for Monte Carlo sampling or stochastic models, but not for crypto.底线 - 将其用于蒙特卡罗采样或随机模型,但不适用于加密。

(*) - Actually, Pierre L'Ecuyer , who is considered one of the foremost researchers on pseudo-random number generation, is not a fan of MT even for Monte Carlo usage. (*) - 实际上,被认为是伪随机数生成领域最重要的研究人员之一的Pierre L'Ecuyer并不是 MT 的粉丝,即使是使用 Monte Carlo 也是如此。 He has shown that while the full cycle is uniformly distributed, zeros in the the internal state tend to be persistent and the generator can get "stuck" for sizeable sub-periods in non-uniform subsequences.他已经表明,虽然整个周期是均匀分布的,但内部状态中的零往往是持久的,并且生成器可能会在非均匀子序列中的相当大的子周期中“卡住”。 He collaborated with the creator of Mersenne Twister to fix these issues in the WELL generator .他与 Mersenne Twister 的创建者合作解决了WELL 生成器中的这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM