SecureRandom 的 setSeed() 方法在 Java 中
随机对象可以使用 java.security.SecureRandom 类中的 setSeed() 方法重新播种。此方法需要一个参数(即所需的种子字节数组),并返回重新播种的随机对象。
演示此方法的一个程序如下所示 −
示例
import java.security.*;
import java.util.*;
public class Demo {
public static void main(String[] argv) {
try {
SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
String str = "Apple";
byte[] arrB = str.getBytes();
sRandom.setSeed(arrB);
byte[] arrSeed = sRandom.getSeed(5);
System.out.println("The reseeded Byte array is:");
for (int i = 0; i < arrSeed.length; i++) {
System.out.print(arrSeed[i] + " ");
}
} catch (NoSuchAlgorithmException e) {
System.out.println("Error!!! NoSuchAlgorithmException");
} catch (ProviderException e) {
System.out.println("Error!!! ProviderException");
}
}
}输出
The reseeded Byte array is: -88 71 -32 -124 55
现在,我们来了解上面的程序。
setSeed() 方法用于重新播种随机对象。然后显示重新播种的字节数组。演示此操作的代码片段如下 −
try {
SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
String str = "Apple";
byte[] arrB = str.getBytes();
sRandom.setSeed(arrB);
byte[] arrSeed = sRandom.getSeed(5);
System.out.println("The reseeded Byte array is:");
for (int i = 0; i < arrSeed.length; i++) {
System.out.print(arrSeed[i] + " ");
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP