AlgorithmParameterGenerator generateParameters() method in Java


The parameters can be generated using the method generateParameters() in the class java.security.AlgorithmParameterGenerator. This method requires no parameters and it returns the AlgorithmParameter object.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.security.*;
import java.util.*;
public class Demo {
   public static void main(String[] argv) {
      try {
         AlgorithmParameterGenerator apGenerator = AlgorithmParameterGenerator.getInstance("DiffieHellman");
         apGenerator.init(1024);
         AlgorithmParameters aParameters = apGenerator.generateParameters();
         System.out.println(aParameters);
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      } catch (ProviderException e) {
         System.out.println("Error!!! ProviderException");
      }
   }
}

Output

SunJCE Diffie-Hellman Parameters:
p:
   95d00415 5cf1e7a8 edbfa631 bff2bcdd 653476f4 3d9d1a88 d0e7fd2f 1503135a
   2b843a76 fd6850b6 f2feae0a f896f5da b792d293 3ed80ac8 0a1a11bc 5e031ed4
   2421e7d5 f114760e 67828644 7f281e8a 6f142ba7 6de507d2 3ad44eb9 604b16d7
   88b8e0e4 2df6a6f9 3d7cc162 060dcb7a 939b67f8 2bee8059 de68fe43 511f1481
g:
   280b4ebf 3cede4c5 e598b98b ccdaaad1 2336a722 d18d79f7 6728c78f 9ed171f3
   76b5fdbd 1240a058 75796539 9369f6c1 9b7867e9 6b50095e b7ec5135 69c3e305
   6f156c6e b29bb9f9 898ea6d5 14acc26c 74d16555 bf45dcdb 8c6655e9 48df76c1
   c17c4aea ac3d0ecf 0744bedb bfcb865b f41ea75c 7c101acd 83768114 a4d72f57
l:
   1023

Now let us understand the above program.

The method generateParameters() is used to generate the parameters in the form of AlgorithmParameters object. Then the parameters are displayed. If the algorithm name is wrong, then the exception NoSuchAlgorithmException is thrown. A code snippet that demonstrates is given as follows −

try {
   AlgorithmParameterGenerator apGenerator = AlgorithmParameterGenerator.getInstance("DiffieHellman");
   apGenerator.init(1024);
   AlgorithmParameters aParameters = apGenerator.generateParameters();
   System.out.println(aParameters);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
} catch (ProviderException e) {
   System.out.println("Error!!! ProviderException");
}

Updated on: 30-Jul-2019

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements