- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SecureRandom getInstance() method in Java
A SecureRandom object can be obtained using the getInstance() method in class java.security.SecureRandom. This SecureRandom object is useful in implementing the Random Number Generator (RNG) algorithm that is specified.
The getInstance() method requires a single parameter i.e. the Random Number Generator (RNG) algorithm and it returns the SecureRandom object.
A program that demonstrates this is given as follows −
Example
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"); } } }
Output
The Byte array before the operation is: [65, 112, 112, 108, 101] The Byte array after the operation is: [10, 60, 119, -12, -103]
Now let us understand the above program.
The getInstance() method is used to obtain the SecureRandom object sRandom. Using this, the byte array before and after the operation is displayed. A code snippet that demonstrates is given as follows −
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)); }
- Related Articles
- KeyPairGenerator getInstance() method in Java
- Java Signature getInstance() method
- SecureRandom generateSeed() method in Java
- SecureRandom getAlgorithm() method in Java
- SecureRandom getProvider() method in Java
- SecureRandom setSeed() method in Java
- SecureRandom getSeed() method in Java
- SecureRandom nextBytes() method in Java
- Java Signature getInstance() method with Examples
- Collections.replaceAll() method and List.replaceAll() method in Java
- Method overloading in Java
- Method overriding in Java
- BigInteger.isProbablePrime() method in Java
- Integer.lowestOneBit() method in Java
- Integer.numberOfLeadingZeros() method in Java

Advertisements