public final KeySpec getKeySpec(SecretKey key, Class<?> keySpec)
            throws InvalidKeySpecException {
        if (serviceIterator == null) {
            return spi.engineGetKeySpec(key, keySpec);
        }
        Exception failure = null;
        SecretKeyFactorySpi mySpi = spi;
        do {
            try {
                return mySpi.engineGetKeySpec(key, keySpec);
            } catch (Exception e) {
                if (failure == null) {
                    failure = e;
                }
                mySpi = nextSpi(mySpi);
            }
        } while (mySpi != null);
        if (failure instanceof InvalidKeySpecException) {
            throw (InvalidKeySpecException)failure;
        }
        throw new InvalidKeySpecException
                ("Could not get key spec", failure);
    }
public final KeySpec getKeySpec ( SecretKey key , Class < ? > keySpec ) throws InvalidKeySpecException { if ( serviceIterator == null ) { return spi . engineGetKeySpec ( key , keySpec ) ; } Exception failure = null ; SecretKeyFactorySpi mySpi = spi ; do { try { return mySpi . engineGetKeySpec ( key , keySpec ) ; } catch ( Exception e ) { if ( failure == null ) { failure = e ; } mySpi = nextSpi ( mySpi ) ; } } while ( mySpi != null ) ; if ( failure instanceof InvalidKeySpecException ) { throw ( InvalidKeySpecException ) failure ; } throw new InvalidKeySpecException ( "Could not get key spec" , failure ) ; }
Returns a specification (key material) of the given key object
in the requested format.

@param key the key
@param keySpec the requested format in which the key material shall be
returned

@return the underlying key specification (key material) in the
requested format

@exception InvalidKeySpecException if the requested key specification is
inappropriate for the given key (e.g., the algorithms associated with
<code>key</code> and <code>keySpec</code> do not match, or
<code>key</code> references a key on a cryptographic hardware device
whereas <code>keySpec</code> is the specification of a software-based
key), or the given key cannot be dealt with
(e.g., the given key has an algorithm or format not supported by this
secret-key factory).