当前位置:网站首页>SecureRandom那些事|真伪随机数
SecureRandom那些事|真伪随机数
2022-07-05 19:46:00 【步尔斯特】
前面的几篇文章,介绍了随机数的几种生成方式,那么什么是伪随机数呢?
所谓伪随机数,是指只要给定⼀个初始的种⼦,产⽣的随机数序列是完全⼀样的。
for (int i = 0; i < 10; i++) {
Random random = new Random(123);
int randomValue = random.nextInt(10);
System.out.println(randomValue);
}

当我们初始化Random不给定种子的时候,会默认给定当前的时间作为种子。
/** * Creates a new random number generator. This constructor sets * the seed of the random number generator to a value very likely * to be distinct from any other invocation of this constructor. */
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
跟踪
* @return the current value of the running Java Virtual Machine's
* high-resolution time source, in nanoseconds
* @since 1.5
*/
public static native long nanoTime();
那么,什么是真随机数呢?
真正的真随机数只能通过量⼦⼒学原理来获取,⽽我们想要的是⼀个不可预测的安全的随机
数,SecureRandom就是⽤来创建安全的随机数的:
SecureRandom sr = new SecureRandom();
System.out.println(sr.nextInt(100));
SecureRandom⽆法指定种⼦,它使⽤RNG(random number generator)算法。JDK的SecureRandom实际上有多种不同的底层实现,有的使⽤安全随机种⼦加上伪随机数算法来产⽣安全的随机数,有的使⽤真正的随机数⽣成器。
实际使⽤的时候,可以优先获取⾼强度的安全随机数⽣成器,如果没有提供,再使⽤普通等级的安全随机数⽣成器:
import java.util.Arrays;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
public class Main {
public static void main(String[] args) {
SecureRandom sr = null;
try {
// 获取⾼强度安全随机数⽣成器
sr = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
// 获取普通的安全随机数⽣成器
sr = new SecureRandom();
}
}
SecureRandom的安全性是通过操作系统提供的安全的随机种⼦来⽣成随机数。
这个种⼦是通过CPU的热噪声、读写磁盘的字节、⽹络流量等各种随机事件产⽣的“熵”。
在密码学中,安全的随机数⾮常重要。如果使⽤不安全的伪随机数,所有加密体系都将被攻破。
因此,时刻牢记必须使⽤SecureRandom来产⽣安全的随机数。
边栏推荐
- Common - Hero Minesweeper
- Fundamentals of shell programming (Chapter 9: loop)
- Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
- Hiengine: comparable to the local cloud native memory database engine
- Android面试,android音视频开发
- Thread pool parameters and reasonable settings
- Fuzor 2020軟件安裝包下載及安裝教程
- Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
- 【obs】libobs-winrt :CreateDispatcherQueueController
- S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
猜你喜欢

全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云

使用 RepositoryProvider简化父子组件的传值

C#应用程序界面开发基础——窗体控制(5)——分组类控件
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Django uses mysqlclient service to connect and write to the database

No matter how busy you are, you can't forget safety
![[FAQ] summary of common causes and solutions of Huawei account service error 907135701](/img/1d/0e716533237c0e4463f5d6357395bd.png)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701

如何在2022年更明智地应用智能合约?

众昂矿业:2022年全球萤石行业市场供给现状分析

Force buckle 1200 Minimum absolute difference
随机推荐
What are general items
[AI framework basic technology] automatic derivation mechanism (autograd)
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
acm入门day1
Fundamentals of shell programming (Part 8: branch statements -case in)
安信证券在网上开户安全吗?
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
What is the core value of testing?
大厂面试必备技能,2022Android不死我不倒
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
Where is the operation of new bonds? Is it safer and more reliable to open an account
【obs】libobs-winrt :CreateDispatcherQueueController
安卓面试宝典,2022Android面试笔试总结
aggregate
How MySQL queries and modifies JSON data
【硬核干货】数据分析哪家强?选Pandas还是选SQL
How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI