create DHParameterSpec - Java Security

Java examples for Security:Key

Description

create DHParameterSpec

Demo Code


//package com.java2s;

import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidParameterSpecException;
import javax.crypto.spec.DHParameterSpec;

public class Main {
    /**/*from w  ww  . ja  v  a 2s  .  c  om*/
     * 
     * @param keyBitSize (must be multiple of 64, and can only range from 512 to 1024 (inclusive))
     * @return The G(base of exponential function) and P(prime for mod function).
     * @throws InvalidParameterSpecException
     * @throws NoSuchAlgorithmException
     */
    public final static DHParameterSpec createDHParamSpec(int keyBitSize)
            throws InvalidParameterSpecException, NoSuchAlgorithmException {
        AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator
                .getInstance("DH");
        paramGen.init(keyBitSize);

        AlgorithmParameters params = paramGen.generateParameters();
        DHParameterSpec paramSpec = params
                .getParameterSpec(DHParameterSpec.class);

        return paramSpec;
    }
}

Related Tutorials