getInstance( String algorithm )
The getInstance() method of java.security.SecureRandom class is used to return a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.
This method traverses the list of registered security Providers, starting with the most preferred Provider. A new SecureRandom object encapsulating the SecureRandomSpi implementation from the first Provider that supports the specified algorithm is returned.
Syntax:
public static SecureRandom
getInstance( String algorithm )
throws NoSuchAlgorithmException
Parameters: This method takes the standard RNG algorithm as a parameter.
Return Value:This method returns the new SecureRandom object .
Exception: This method throws NoSuchAlgorithmException – if no Provider supports a SecureRandomSpi implementation for the specified algorithm.
Note:
- The programs will not run on online IDE.
- Every time Secure Random class will generate random output.
Below are the examples to illustrate the getInstance() method:
Example 1:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG" );
String str = "Tajmahal" ;
byte [] b = str.getBytes();
System.out.println( "Byte array before operation : "
+ Arrays.toString(b));
sr.nextBytes(b);
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);
}
}
}
|
Output:
Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [124, -66, -62, -5, -71, -4, 30, 16]
Example 2:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
System.out.println( "Trying to get the instance of TAJMAHAL" );
SecureRandom sr = SecureRandom.getInstance( "TAJMAHAL" );
String str = "Tajmahal" ;
byte [] b = str.getBytes();
System.out.println( "Byte array before operation : "
+ Arrays.toString(b));
sr.nextBytes(b);
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);
}
}
}
|
Output:
Trying to get the instance of TAJMAHAL
Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL SecureRandom not available
getInstance(String algorithm, Provider provider)
The getInstance() method of java.security.SecureRandom class is used to return a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.
A new SecureRandom object encapsulating the SecureRandomSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list.
The returned SecureRandom object has not been seeded. To seed the returned object, call the setSeed method. If setSeed is not called, the first call to nextBytes will force the SecureRandom object to seed itself. This self-seeding will not occur if setSeed was previously called.
Syntax:
public static SecureRandom
getInstance( String algorithm, Provider provider )
throws NoSuchAlgorithmException
Parameters: This method takes following argument as a parameter
- algorithm –the name of the RNG algorithm.
- provider – the provider.
Return Value: This method returns the new SecureRandom object .
Exception: This method throws the following Exception
Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [109, 55, 116, -15, -83, 126, -128, 88]
Note: The following program produce the following Exception in online IDE
Exception thrown : java.security.ProviderException: init failed
Example 2:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
SecureRandom sr1 = new SecureRandom( new byte [] { 1 , 2 , 3 , 4 });
Provider pd = sr1.getProvider();
System.out.println( "Trying to getting the instance of TAJMAHAL " );
SecureRandom sr = SecureRandom.getInstance( "TAJMAHAL" , pd);
String str = "Tajmahal" ;
byte [] b = str.getBytes();
System.out.println( "Byte array before operation : "
+ Arrays.toString(b));
sr.nextBytes(b);
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);
}
}
}
|
Output:
Trying to getting the instance of TAJMAHAL
Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: TAJMAHAL for provider SUN
getInstance(String algorithm, String provider)
The getInstance() method of java.security.SecureRandom class is used to return a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.
A new SecureRandom object encapsulating the SecureRandomSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list.
Syntax:
public static SecureRandom
getInstance( String algorithm, String provider )
throws NoSuchAlgorithmException, NoSuchProviderException
Parameters: This method takes following argument as a parameter
- algorithm – the name of the RNG algorithm. See the SecureRandom section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard RNG algorithm names.
- provider – the name of the provider.
Return Value: This method returns the new SecureRandom object .
Exception: This method throws the following Exception
- NoSuchAlgorithmException – if a SecureRandomSpi implementation for the specified algorithm is not available from the specified provider.
- NoSuchProviderException – if the specified provider is not registered in the security provider list.
- IllegalArgumentException – if the provider name is null or empty.
Note:
- The programs will not run on online IDE.
- Every time Secure Random class will generate random output.
Below are the examples to illustrate the getInstance() method:
Example 1:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void
main(String[] argv)
throws NoSuchAlgorithmException,
NoSuchProviderException
{
try {
SecureRandom sr1 = new SecureRandom( new byte [] { 1 , 2 , 3 , 4 });
Provider pd = sr1.getProvider();
String provider = pd.getName();
String algo = sr1.getAlgorithm();
SecureRandom sr = SecureRandom.getInstance(algo, provider);
String str = "Tajmahal" ;
byte [] b = str.getBytes();
System.out.println( "Byte array before operation : "
+ Arrays.toString(b));
sr.nextBytes(b);
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);
}
}
}
|
Output:
Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [-11, 81, 39, 67, -95, -51, 115, -18]
Example 2:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void
main(String[] argv)
throws NoSuchAlgorithmException,
NoSuchProviderException
{
try {
SecureRandom sr1 = new SecureRandom( new byte [] { 1 , 2 , 3 , 4 });
Provider pd = sr1.getProvider();
String provider = pd.getName();
System.out.println( "Trying to take TAJMAHAL as a algorithm" );
SecureRandom sr = SecureRandom.getInstance( "TAJMAHAL" , provider);
String str = "Tajmahal" ;
byte [] b = str.getBytes();
System.out.println( "Byte array before operation : "
+ Arrays.toString(b));
sr.nextBytes(b);
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);
}
}
}
|
Output:
Trying to take TAJMAHAL as a algorithm
Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: TAJMAHAL for provider SUN
Example 3:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void
main(String[] argv)
throws NoSuchAlgorithmException,
NoSuchProviderException
{
try {
SecureRandom sr1 = new SecureRandom( new byte [] { 1 , 2 , 3 , 4 });
String algo = sr1.getAlgorithm();
System.out.println( "Trying to taking MOON as a provider" );
SecureRandom sr = SecureRandom.getInstance(algo, "MOON" );
String str = "Tajmahal" ;
byte [] b = str.getBytes();
System.out.println( "Byte array before operation : "
+ Arrays.toString(b));
sr.nextBytes(b);
System.out.println( "Byte array after operation : "
+ Arrays.toString(b));
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (NoSuchProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Trying to taking MOON as a provider
Exception thrown : java.security.NoSuchProviderException: no such provider: MOON
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...