当前位置:网站首页>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来产⽣安全的随机数。
边栏推荐
- 力扣 1200. 最小绝对差
- Reflection and imagination on the notation like tool
- Fundamentals of deep learning convolutional neural network (CNN)
- Postman core function analysis - parameterization and test report
- 建议收藏,我的腾讯Android面试经历分享
- Concept and syntax of function
- Common operators and operator priority
- What is the core value of testing?
- 使用 RepositoryProvider简化父子组件的传值
- 线程池参数及合理设置
猜你喜欢
Reinforcement learning - learning notes 4 | actor critical
成功入职百度月薪35K,2022Android开发面试解答
Force buckle 729 My schedule I
40000 word Wenshuo operator new & operator delete
微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
The problem of returning the longtext field in MySQL and its solution
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
acm入门day1
MMO項目學習一:預熱
深度学习 卷积神经网络(CNN)基础
随机推荐
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
JMeter 常用的几种断言方法,你会了吗?
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
力扣 729. 我的日程安排表 I
测试的核心价值到底是什么?
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
gst-launch的-v参数
Force buckle 1200 Minimum absolute difference
Gstreamer中的task
软件测试是干什么的?学习有啥要求?
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
redis集群模拟消息队列
openh264解码数据流向分析
如何安全快速地从 Centos迁移到openEuler
微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
MMO項目學習一:預熱
【obs】libobs-winrt :CreateDispatcherQueueController
关于 Notion-Like 工具的反思和畅想
Is it safe for China Galaxy Securities to open an account? Securities account opening
The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)