當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java SecureRandom nextBytes()用法及代碼示例


java.security.SecureRandom類的nextBytes()方法用於生成user-specified個隨機字節。

如果以前未發生過對setSeed的調用,則對此方法的第一次調用將強製此SecureRandom對象播種自身。如果先前調用過setSeed,則不會發生此self-seeding。

用法:


public void nextBytes(byte[] bytes)

參數:此方法將要使用隨機字節填充的數組作為參數。

注意:

  1. 該程序將無法在在線IDE上運行。
  2. 每次Secure Random類將生成隨機輸出。

下麵是說明nextBytes()方法的示例:

示例1:

// Java program to demonstrate 
// nextBytes() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating the object of SecureRandom 
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); 
  
            // Declaring the string variable 
            String str = "Tajmahal"; 
  
            // Declaring the byte Array 
            // converting string into byte 
            byte[] b = str.getBytes(); 
  
            // printing the byte array 
            System.out.println("Byte array before operation : " + Arrays.toString(b)); 
  
            // generating user-specified number of random bytes 
            // using nextBytes() method 
            sr.nextBytes(b); 
  
            // printing the new byte array 
            System.out.println("Byte array after operation : " + Arrays.toString(b)); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}

輸出:

Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [-79, -110, -18, -31, -54, -36, 63, -61]

注意:以下程序在GeeksForGeeks IDE上引發以下異常,但它將在任何命令提示符(JDK)上高效運行

Exception thrown : java.security.ProviderException: init failed

示例2:

// Java program to demonstrate 
// nextBytes() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating the object of SecureRandom 
            SecureRandom sr = new SecureRandom(new byte[] { 1, 2, 3, 4 }); 
  
            // Declaring the string variable 
            String str = "Tajmahal"; 
  
            // Declaring the byte Array b 
            byte[] b = str.getBytes(); 
  
            // printing the byte array 
            System.out.println("Byte array before operation : " + Arrays.toString(b)); 
  
            // generating user-specified number of random bytes 
            // using nextBytes() method 
            sr.nextBytes(b); 
  
            // printing the new byte array 
            System.out.println("Byte array after operation : " + Arrays.toString(b)); 
        } 
  
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}

輸出:

Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [-14, 77, 123, 121, 116, 50, -89, -86]


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 SecureRandom nextBytes() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。