当前位置:网站首页>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来产⽣安全的随机数。
边栏推荐
- 不愧是大佬,字节大牛耗时八个月又一力作
- 40000 word Wenshuo operator new & operator delete
- 如何安全快速地从 Centos迁移到openEuler
- Add data to excel small and medium-sized cases through poi
- PG基础篇--逻辑结构管理(用户及权限管理)
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- [C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
- 安信证券在网上开户安全吗?
- No matter how busy you are, you can't forget safety
- Xaas trap: all things serve (possible) is not what it really needs
猜你喜欢

测试的核心价值到底是什么?

集合

Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)

S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤

软件测试工程师是做什么的?待遇前景怎么样?

强化学习-学习笔记4 | Actor-Critic

关于 Notion-Like 工具的反思和畅想

Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
安卓面试宝典,2022Android面试笔试总结

webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
随机推荐
【obs】libobs-winrt :CreateDispatcherQueueController
Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
The problem of returning the longtext field in MySQL and its solution
That's awesome. It's enough to read this article
Information / data
Mysql如何对json数据进行查询及修改
多分支结构
完爆面试官,一线互联网企业高级Android工程师面试题大全
从零实现深度学习框架——LSTM从理论到实战【实战】
openh264解码数据流向分析
如何在2022年更明智地应用智能合约?
成功入职百度月薪35K,2022Android开发面试解答
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
Postman核心功能解析-参数化和测试报告
Add data to excel small and medium-sized cases through poi
毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
What is the core value of testing?
信息/数据