Search in sources :

Example 91 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project jdk8u_jdk by JetBrains.

the class CipherWithWrappingSpi method constructPrivateKey.

/**
     * Construct a private key from its encoding.
     *
     * @param encodedKey the encoding of a private key.
     *
     * @param encodedKeyAlgorithm the algorithm the wrapped key is for.
     *
     * @return a private key constructed from the encodedKey.
     */
private final PrivateKey constructPrivateKey(byte[] encodedKey, String encodedKeyAlgorithm) throws InvalidKeyException, NoSuchAlgorithmException {
    PrivateKey key = null;
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(encodedKeyAlgorithm, SunJCE.getInstance());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
        return keyFactory.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException nsae) {
        // provider which supports this algorithm
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(encodedKeyAlgorithm);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
            key = keyFactory.generatePrivate(keySpec);
        } catch (NoSuchAlgorithmException nsae2) {
            throw new NoSuchAlgorithmException("No installed providers " + "can create keys for the " + encodedKeyAlgorithm + "algorithm");
        } catch (InvalidKeySpecException ikse2) {
        // Should never happen.
        }
    } catch (InvalidKeySpecException ikse) {
    // Should never happen.
    }
    return key;
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 92 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project jdk8u_jdk by JetBrains.

the class CipherWithWrappingSpi method constructPublicKey.

/**
     * Construct a public key from its encoding.
     *
     * @param encodedKey the encoding of a public key.
     *
     * @param encodedKeyAlgorithm the algorithm the encodedKey is for.
     *
     * @return a public key constructed from the encodedKey.
     */
private final PublicKey constructPublicKey(byte[] encodedKey, String encodedKeyAlgorithm) throws InvalidKeyException, NoSuchAlgorithmException {
    PublicKey key = null;
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(encodedKeyAlgorithm, SunJCE.getInstance());
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
        key = keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException nsae) {
        // provider which supports this algorithm
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(encodedKeyAlgorithm);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
            key = keyFactory.generatePublic(keySpec);
        } catch (NoSuchAlgorithmException nsae2) {
            throw new NoSuchAlgorithmException("No installed providers " + "can create keys for the " + encodedKeyAlgorithm + "algorithm");
        } catch (InvalidKeySpecException ikse2) {
        // Should never happen.
        }
    } catch (InvalidKeySpecException ikse) {
    // Should never happen.
    }
    return key;
}
Also used : PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 93 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project jdk8u_jdk by JetBrains.

the class DSAKeyValue method getPublicKey.

/** @inheritDoc */
public PublicKey getPublicKey() throws XMLSecurityException {
    try {
        DSAPublicKeySpec pkspec = new DSAPublicKeySpec(this.getBigIntegerFromChildElement(Constants._TAG_Y, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_P, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_Q, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_G, Constants.SignatureSpecNS));
        KeyFactory dsaFactory = KeyFactory.getInstance("DSA");
        PublicKey pk = dsaFactory.generatePublic(pkspec);
        return pk;
    } catch (NoSuchAlgorithmException ex) {
        throw new XMLSecurityException("empty", ex);
    } catch (InvalidKeySpecException ex) {
        throw new XMLSecurityException("empty", ex);
    }
}
Also used : PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 94 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project jdk8u_jdk by JetBrains.

the class RSAKeyValue method getPublicKey.

/** @inheritDoc */
public PublicKey getPublicKey() throws XMLSecurityException {
    try {
        KeyFactory rsaFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec rsaKeyspec = new RSAPublicKeySpec(this.getBigIntegerFromChildElement(Constants._TAG_MODULUS, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_EXPONENT, Constants.SignatureSpecNS));
        PublicKey pk = rsaFactory.generatePublic(rsaKeyspec);
        return pk;
    } catch (NoSuchAlgorithmException ex) {
        throw new XMLSecurityException("empty", ex);
    } catch (InvalidKeySpecException ex) {
        throw new XMLSecurityException("empty", ex);
    }
}
Also used : PublicKey(java.security.PublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 95 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project jdk8u_jdk by JetBrains.

the class DEREncodedKeyValue method getEncodedDER.

/**
     * Method getEncodedDER
     *
     * @return the public key
     * @throws XMLSecurityException
     */
protected byte[] getEncodedDER(PublicKey publicKey) throws XMLSecurityException {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(publicKey.getAlgorithm());
        X509EncodedKeySpec keySpec = keyFactory.getKeySpec(publicKey, X509EncodedKeySpec.class);
        return keySpec.getEncoded();
    } catch (NoSuchAlgorithmException e) {
        Object[] exArgs = { publicKey.getAlgorithm(), publicKey.getFormat(), publicKey.getClass().getName() };
        throw new XMLSecurityException("DEREncodedKeyValue.UnsupportedPublicKey", exArgs, e);
    } catch (InvalidKeySpecException e) {
        Object[] exArgs = { publicKey.getAlgorithm(), publicKey.getFormat(), publicKey.getClass().getName() };
        throw new XMLSecurityException("DEREncodedKeyValue.UnsupportedPublicKey", exArgs, e);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Aggregations

InvalidKeySpecException (java.security.spec.InvalidKeySpecException)237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)147 KeyFactory (java.security.KeyFactory)99 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)93 InvalidKeyException (java.security.InvalidKeyException)62 PublicKey (java.security.PublicKey)57 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)56 IOException (java.io.IOException)51 PrivateKey (java.security.PrivateKey)40 SecretKeyFactory (javax.crypto.SecretKeyFactory)30 PBEKeySpec (javax.crypto.spec.PBEKeySpec)27 SignatureException (java.security.SignatureException)22 UnsupportedEncodingException (java.io.UnsupportedEncodingException)21 KeySpec (java.security.spec.KeySpec)19 BadPaddingException (javax.crypto.BadPaddingException)19 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)19 BigInteger (java.math.BigInteger)17 SecretKeySpec (javax.crypto.spec.SecretKeySpec)16 NoSuchProviderException (java.security.NoSuchProviderException)15 RSAPublicKey (java.security.interfaces.RSAPublicKey)15