类javax.crypto.spec.DHParameterSpec源码实例Demo

下面列出了怎么用javax.crypto.spec.DHParameterSpec的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: Bytecoder   文件: ParameterCache.java
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
 
源代码2 项目: openjsse   文件: SupportedGroupsExtension.java
static DHParameterSpec getDHParameterSpec(NamedGroup namedGroup) {
    if (namedGroup.type != NamedGroupType.NAMED_GROUP_FFDHE) {
        throw new RuntimeException(
                "Not a named DH group: " + namedGroup);
    }

    AlgorithmParameters params = namedGroupParams.get(namedGroup);
    if (params == null) {
        throw new RuntimeException(
                "Not a supported DH named group: " + namedGroup);
    }

    try {
        return params.getParameterSpec(DHParameterSpec.class);
    } catch (InvalidParameterSpecException ipse) {
        // should be unlikely
        return getPredefinedDHParameterSpec(namedGroup);
    }
}
 
源代码3 项目: dragonwell8_jdk   文件: DHKeyPairGenerator.java
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
源代码4 项目: TencentKona-8   文件: ParameterCache.java
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
 
源代码5 项目: red5-server-common   文件: RTMPHandshake.java
/**
 * Creates a Diffie-Hellman key pair.
 * 
 * @return dh keypair
 */
protected KeyPair generateKeyPair() {
    KeyPair keyPair = null;
    DHParameterSpec keySpec = new DHParameterSpec(DH_MODULUS, DH_BASE);
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
        keyGen.initialize(keySpec);
        keyPair = keyGen.generateKeyPair();
        keyAgreement = KeyAgreement.getInstance("DH");
        // key agreement is initialized with "this" ends private key
        keyAgreement.init(keyPair.getPrivate());
    } catch (Exception e) {
        log.error("Error generating keypair", e);
    }
    return keyPair;
}
 
源代码6 项目: ripple-lib-java   文件: KeyPairGeneratorSpi.java
public void initialize(
    AlgorithmParameterSpec params,
    SecureRandom random)
    throws InvalidAlgorithmParameterException
{
    if (!(params instanceof DHParameterSpec))
    {
        throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec");
    }
    DHParameterSpec dhParams = (DHParameterSpec)params;

    param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));

    engine.init(param);
    initialised = true;
}
 
源代码7 项目: TencentKona-8   文件: DHKeyPairGenerator.java
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
源代码8 项目: openjdk-jdk9   文件: DHKeyPairGenerator.java
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param algParams the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
源代码9 项目: openjdk-jdk8u   文件: ParameterCache.java
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
 
源代码10 项目: hottub   文件: ParameterCache.java
/**
 * Return DH parameters for the given keylength. Uses cache if possible,
 * generates new parameters and adds them to the cache otherwise.
 */
public static DHParameterSpec getDHParameterSpec(int keyLength,
        SecureRandom random)
        throws NoSuchAlgorithmException, InvalidParameterSpecException {
    DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
    if (spec != null) {
        return spec;
    }
    AlgorithmParameterGenerator gen =
            AlgorithmParameterGenerator.getInstance("DH");
    gen.init(keyLength, random);
    AlgorithmParameters params = gen.generateParameters();
    spec = params.getParameterSpec(DHParameterSpec.class);
    dhCache.put(Integer.valueOf(keyLength), spec);
    return spec;
}
 
源代码11 项目: openjdk-jdk8u   文件: DHKeyPairGenerator.java
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param algParams the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
源代码12 项目: jdk8u-jdk   文件: DHParameters.java
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
        if (!(paramSpec instanceof DHParameterSpec)) {
            throw new InvalidParameterSpecException
                ("Inappropriate parameter specification");
        }
        this.p = ((DHParameterSpec)paramSpec).getP();
        this.g = ((DHParameterSpec)paramSpec).getG();
        this.l = ((DHParameterSpec)paramSpec).getL();
}
 
源代码13 项目: ripple-lib-java   文件: KeyAgreementSpi.java
protected void engineInit(
    Key                     key,
    AlgorithmParameterSpec  params,
    SecureRandom            random) 
    throws InvalidKeyException, InvalidAlgorithmParameterException
{
    if (!(key instanceof DHPrivateKey))
    {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
    }
    DHPrivateKey    privKey = (DHPrivateKey)key;

    if (params != null)
    {
        if (!(params instanceof DHParameterSpec))
        {
            throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
        }
        DHParameterSpec p = (DHParameterSpec)params;

        this.p = p.getP();
        this.g = p.getG();
    }
    else
    {
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
    }

    this.x = this.result = privKey.getX();
}
 
源代码14 项目: openjsse   文件: DHKeyExchange.java
private static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DiffieHellman");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        // unlikely
        throw new RuntimeException("Unable to get DHPublicKeySpec", e);
    }
}
 
源代码15 项目: jdk8u_jdk   文件: KeyUtil.java
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
源代码16 项目: openjdk-8-source   文件: DHKeyPairGenerator.java
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    if ((pSize < 512) || (pSize > 2048) ||
        (pSize % 64 != 0)) {
        throw new InvalidAlgorithmParameterException
            ("Prime size must be multiple of 64, and can only range "
             + "from 512 to 2048 (inclusive)");
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
源代码17 项目: openjdk-8   文件: DHParameters.java
protected <T extends AlgorithmParameterSpec>
    T engineGetParameterSpec(Class<T> paramSpec)
    throws InvalidParameterSpecException {

    if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
        return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));
    } else {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter Specification");
    }
}
 
源代码18 项目: hottub   文件: DHPublicKey.java
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
源代码19 项目: openid4java   文件: DiffieHellmanSessionTest.java
public void testGenerateKeyPairSha1Default()
{
    DHParameterSpec parameterSpec = DiffieHellmanSession.getDefaultParameter();

    KeyPair keyPair = DiffieHellmanSession.generateKeyPair(parameterSpec);

    assertNotNull(keyPair);
}
 
源代码20 项目: openjdk-8-source   文件: DHParameters.java
protected <T extends AlgorithmParameterSpec>
    T engineGetParameterSpec(Class<T> paramSpec)
    throws InvalidParameterSpecException {

    if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
        return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));
    } else {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter Specification");
    }
}
 
源代码21 项目: dragonwell8_jdk   文件: DHParameters.java
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
        if (!(paramSpec instanceof DHParameterSpec)) {
            throw new InvalidParameterSpecException
                ("Inappropriate parameter specification");
        }
        this.p = ((DHParameterSpec)paramSpec).getP();
        this.g = ((DHParameterSpec)paramSpec).getG();
        this.l = ((DHParameterSpec)paramSpec).getL();
}
 
源代码22 项目: jdk8u_jdk   文件: DHPublicKey.java
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
源代码23 项目: Bytecoder   文件: DHPrivateKey.java
public boolean equals(Object obj) {
    if (this == obj) return true;

    if (!(obj instanceof javax.crypto.interfaces.DHPrivateKey)) {
        return false;
    }
    javax.crypto.interfaces.DHPrivateKey other =
            (javax.crypto.interfaces.DHPrivateKey) obj;
    DHParameterSpec otherParams = other.getParams();
    return ((this.x.compareTo(other.getX()) == 0) &&
            (this.p.compareTo(otherParams.getP()) == 0) &&
            (this.g.compareTo(otherParams.getG()) == 0));
}
 
源代码24 项目: jdk8u-dev-jdk   文件: DHPrivateKey.java
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
源代码25 项目: Bytecoder   文件: DHParameters.java
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
        if (!(paramSpec instanceof DHParameterSpec)) {
            throw new InvalidParameterSpecException
                ("Inappropriate parameter specification");
        }
        this.p = ((DHParameterSpec)paramSpec).getP();
        this.g = ((DHParameterSpec)paramSpec).getG();
        this.l = ((DHParameterSpec)paramSpec).getL();
}
 
源代码26 项目: dragonwell8_jdk   文件: DHPublicKey.java
public boolean equals(Object obj) {
    if (this == obj) return true;

    if (!(obj instanceof javax.crypto.interfaces.DHPublicKey)) {
        return false;
    }

    javax.crypto.interfaces.DHPublicKey other =
        (javax.crypto.interfaces.DHPublicKey) obj;
    DHParameterSpec otherParams = other.getParams();
    return ((this.y.compareTo(other.getY()) == 0) &&
            (this.p.compareTo(otherParams.getP()) == 0) &&
            (this.g.compareTo(otherParams.getG()) == 0));
}
 
源代码27 项目: RipplePower   文件: AlgorithmParametersSpi.java
protected AlgorithmParameterSpec localEngineGetParameterSpec(
    Class paramSpec)
    throws InvalidParameterSpecException
{
    if (paramSpec == ElGamalParameterSpec.class)
    {
        return currentSpec;
    }
    else if (paramSpec == DHParameterSpec.class)
    {
        return new DHParameterSpec(currentSpec.getP(), currentSpec.getG());
    }

    throw new InvalidParameterSpecException("unknown parameter spec passed to ElGamal parameters object.");
}
 
源代码28 项目: jdk8u-jdk   文件: DHParameters.java
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
        if (!(paramSpec instanceof DHParameterSpec)) {
            throw new InvalidParameterSpecException
                ("Inappropriate parameter specification");
        }
        this.p = ((DHParameterSpec)paramSpec).getP();
        this.g = ((DHParameterSpec)paramSpec).getG();
        this.l = ((DHParameterSpec)paramSpec).getL();
}
 
protected AlgorithmParameters engineGenerateParameters()
{
    ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();

    if (random != null)
    {
        pGen.init(strength, 20, random);
    }
    else
    {
        pGen.init(strength, 20, new SecureRandom());
    }

    ElGamalParameters p = pGen.generateParameters();

    AlgorithmParameters params;

    try
    {
        params = createParametersInstance("ElGamal");
        params.init(new DHParameterSpec(p.getP(), p.getG(), l));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e.getMessage());
    }

    return params;
}
 
源代码30 项目: RipplePower   文件: AlgorithmParametersSpi.java
protected AlgorithmParameterSpec localEngineGetParameterSpec(
    Class paramSpec) 
    throws InvalidParameterSpecException
{
    if (paramSpec == DHParameterSpec.class)
    {
        return currentSpec;
    }

    throw new InvalidParameterSpecException("unknown parameter spec passed to DH parameters object.");
}