java securerandom使用_Java中的SecureRandom nextBytes()方法
用戶指定的隨機(jī)字節(jié)數(shù)可以使用nextBytes()類java.security.SecureRandom中的方法獲得。該方法需要一個(gè)參數(shù),即一個(gè)隨機(jī)字節(jié)數(shù)組,它返回用戶指定的隨機(jī)字節(jié)。
演示此的程序如下所示-
示例import?java.security.*;
import?java.util.*;
public?class?Demo?{
public?static?void?main(String[]?argv)?{
try?{
SecureRandom?sRandom?=?SecureRandom.getInstance("SHA1PRNG");
String?s?=?"Apple";
byte[]?arrB?=?s.getBytes();
System.out.println("The?Byte?array?before?the?operation?is:?"?+?Arrays.toString(arrB));
sRandom.nextBytes(arrB);
System.out.println("The?Byte?array?after?the?operation?is:?"?+?Arrays.toString(arrB));
}?catch?(NoSuchAlgorithmException?e)?{
System.out.println("Error!!!?NoSuchAlgorithmException");
}?catch?(ProviderException?e)?{
System.out.println("Error!!!?ProviderException");
}
}
}
輸出結(jié)果The?Byte?array?before?the?operation?is:?[65,?112,?112,?108,?101]
The?Byte?array?after?the?operation?is:?[110,?-119,?-65,?-84,?54]
現(xiàn)在讓我們了解上面的程序。
該nextBytes()方法用于獲取用戶指定的隨機(jī)字節(jié)數(shù)。
使用此方法,可以顯示操作前后的字節(jié)數(shù)組。演示的代碼片段如下-try?{
SecureRandom?sRandom?=?SecureRandom.getInstance("SHA1PRNG");
String?s?=?"Apple";
byte[]?arrB?=?s.getBytes();
System.out.println("The?Byte?array?before?the?operation?is:?"?+?Arrays.toString(arrB));
sRandom.nextBytes(arrB);
System.out.println("The?Byte?array?after?the?operation?is:?"?+?Arrays.toString(arrB));
}
總結(jié)
以上是生活随笔為你收集整理的java securerandom使用_Java中的SecureRandom nextBytes()方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 表 类型_mysql表类型
- 下一篇: matlab+信号+mpf,Python