javax.crypto.spec.DHPrivateKeySpec Java Examples

The following examples show how to use javax.crypto.spec.DHPrivateKeySpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DHKeyFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #2
Source File: DHKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #3
Source File: DHKeyFactory.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #4
Source File: DHKeyFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #5
Source File: DHKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #6
Source File: DHKeyFactory.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #7
Source File: DHKeyFactory.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #8
Source File: DHKeyFactory.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #9
Source File: DHKeyFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #10
Source File: DHKeyFactory.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #11
Source File: DHKeyFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #12
Source File: DHPrivateKeySpecTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DHPrivateKeySpec class testing. Tests the equivalence of parameters
 * specified in the constructor with the values returned by getters.
 */
public void testDHPrivateKeySpec() {
    BigInteger[] xs = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    BigInteger[] ps = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    BigInteger[] gs = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    for (int i=0; i<ps.length; i++) {
        DHPrivateKeySpec dhpks = new DHPrivateKeySpec(xs[i], ps[i], gs[i]);
        assertEquals("The value returned by getX() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getX(), xs[i]);
        assertEquals("The value returned by getP() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getP(), ps[i]);
        assertEquals("The value returned by getG() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getG(), gs[i]);
    }
}
 
Example #13
Source File: DHKeyFactory.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #14
Source File: DHKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #15
Source File: DHKeyFactory.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #16
Source File: DHKeyFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #17
Source File: DHKeyFactory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #18
Source File: DHKeyFactory.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #19
Source File: DHKeyFactory.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #20
Source File: DHKeyFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #21
Source File: DHKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #22
Source File: ValueLinkApi.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Get merchant Private Key
 * @return PrivateKey object for the merchant
 */
public PrivateKey getPrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
    byte[] privateKeyBytes = this.getPrivateKeyBytes();

    // initialize the parameter spec
    DHParameterSpec dhParamSpec = this.getDHParameterSpec();

    // load the private key
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    BigInteger privateKeyInt = new BigInteger(privateKeyBytes);
    DHPrivateKeySpec dhPrivateSpec = new DHPrivateKeySpec(privateKeyInt, dhParamSpec.getP(), dhParamSpec.getG());
    PrivateKey privateKey = keyFactory.generatePrivate(dhPrivateSpec);

    return privateKey;
}
 
Example #23
Source File: ConstantPasswords.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void bad10() throws Exception {
    BigInteger bigInteger = new BigInteger("12345", 5);
    new DSAPrivateKeySpec(bigInteger, null, null, null);
    new DSAPublicKeySpec(bigInteger, null, bigInteger, null); // report once
    new DHPrivateKeySpec(bigInteger, null, null);
    new DHPublicKeySpec(bigInteger, null, null);
    new ECPrivateKeySpec(bigInteger, null);
    new RSAPrivateKeySpec(bigInteger, null);
    new RSAMultiPrimePrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null, null);
    new RSAPrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null);
    new RSAPublicKeySpec(bigInteger, null);
    new DSAPublicKeyImpl(bigInteger, null, null, null);
}
 
Example #24
Source File: KeyFactorySpi.java    From ripple-lib-java with ISC License 5 votes vote down vote up
protected PrivateKey engineGeneratePrivate(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof ElGamalPrivateKeySpec)
    {
        return new BCElGamalPrivateKey((ElGamalPrivateKeySpec)keySpec);
    }
    else if (keySpec instanceof DHPrivateKeySpec)
    {
        return new BCElGamalPrivateKey((DHPrivateKeySpec)keySpec);
    }

    return super.engineGeneratePrivate(keySpec);
}
 
Example #25
Source File: DHKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #26
Source File: DHKeyFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #27
Source File: DHKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #28
Source File: KeyFactorySpi.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
protected PrivateKey engineGeneratePrivate(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof ElGamalPrivateKeySpec)
    {
        return new BCElGamalPrivateKey((ElGamalPrivateKeySpec)keySpec);
    }
    else if (keySpec instanceof DHPrivateKeySpec)
    {
        return new BCElGamalPrivateKey((DHPrivateKeySpec)keySpec);
    }

    return super.engineGeneratePrivate(keySpec);
}
 
Example #29
Source File: DHKeyFactory.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #30
Source File: DHKeyFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}