java securerandom使用_Java中的SecureRandom nextBytes()方法
用戶指定的隨機字節數可以使用nextBytes()類java.security.SecureRandom中的方法獲得。該方法需要一個參數,即一個隨機字節數組,它返回用戶指定的隨機字節。
演示此的程序如下所示-
示例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");
}
}
}
輸出結果The?Byte?array?before?the?operation?is:?[65,?112,?112,?108,?101]
The?Byte?array?after?the?operation?is:?[110,?-119,?-65,?-84,?54]
現在讓我們了解上面的程序。
該nextBytes()方法用于獲取用戶指定的隨機字節數。
使用此方法,可以顯示操作前后的字節數組。演示的代碼片段如下-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));
}
總結
以上是生活随笔為你收集整理的java securerandom使用_Java中的SecureRandom nextBytes()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 表 类型_mysql表类型
- 下一篇: java将图片转byte存入数据库_Ja