Code example for DSAPublicKeySpec

0
            throws InvalidKeySpecException { 
 
        if (keySpec != null) {
            if (keySpec instanceof DSAPublicKeySpec) {
 
                return new DSAPublicKeyImpl((DSAPublicKeySpec) keySpec);
            } 
            if (keySpec instanceof X509EncodedKeySpec) {
 
                return new DSAPublicKeyImpl((X509EncodedKeySpec) keySpec);
            } 
        } 
        throw new InvalidKeySpecException(Messages.getString("security.19D")); //$NON-NLS-1$
    } 
     
    /** 
     * This method returns a specification for the supplied key. 
     *  
     * The specification will be returned in the form of an object of the type 
     * specified by keySpec. 
     *  
     * @param key - 
     *            either DSAPrivateKey or DSAPublicKey 
     * @param keySpec - 
     *            either DSAPrivateKeySpec.class or DSAPublicKeySpec.class 
     *  
     * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec 
     *  
     * @throws InvalidKeySpecException 
     *             if "keySpec" is not a specification for DSAPublicKey or 
     *             DSAPrivateKey 
     */ 
    protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
            throws InvalidKeySpecException { 
 
        BigInteger p, q, g, x, y;
 
        if (key != null) {
            if (keySpec == null) {
                throw new NullPointerException(Messages
                        .getString("security.19E")); //$NON-NLS-1$ 
            } 
            if (key instanceof DSAPrivateKey) {
                DSAPrivateKey privateKey = (DSAPrivateKey) key;
 
                if (keySpec.equals(DSAPrivateKeySpec.class)) {
 
                    x = privateKey.getX();
 
                    DSAParams params = privateKey.getParams();
 
                    p = params.getP();
                    q = params.getQ();
                    g = params.getG();
 
                    return (T) (new DSAPrivateKeySpec(x, p, q, g));
                } 
 
                if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
                    return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
                } 
 
                throw new InvalidKeySpecException(Messages
                        .getString("security.19C")); //$NON-NLS-1$ 
            } 
 
            if (key instanceof DSAPublicKey) {
                DSAPublicKey publicKey = (DSAPublicKey) key;
 
                if (keySpec.equals(DSAPublicKeySpec.class)) {
 
                    y = publicKey.getY();
 
                    DSAParams params = publicKey.getParams();
 
                    p = params.getP();
                    q = params.getQ();
                    g = params.getG();
 
                    return (T) (new DSAPublicKeySpec(y, p, q, g));
                } 
 
                if (keySpec.equals(X509EncodedKeySpec.class)) {
                    return (T) (new X509EncodedKeySpec(key.getEncoded()));
                } 
 
                throw new InvalidKeySpecException(Messages
                        .getString("security.19D")); //$NON-NLS-1$ 
            } 
        } 
        throw new InvalidKeySpecException(Messages.getString("security.19F")); //$NON-NLS-1$
    } 
 
    /** 
     * The method generates a DSAPublicKey object from the provided key.  
     * 
     * @param 
     *    key - a DSAPublicKey object or DSAPrivateKey object. 
     * 
     * @return 
     *    object of the same type as the "key" argument 
     * 
     * @throws InvalidKeyException 
     *     if "key" is neither DSAPublicKey nor DSAPrivateKey 
     */ 
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
 
        if (key != null) {
            if (key instanceof DSAPrivateKey) {
 
                DSAPrivateKey privateKey = (DSAPrivateKey) key;
                DSAParams params = privateKey.getParams();
 
                try { 
                    return engineGeneratePrivate(new DSAPrivateKeySpec(
                            privateKey.getX(), params.getP(), params.getQ(),
                            params.getG()));
                } catch (InvalidKeySpecException e) {
                    // Actually this exception shouldn't be thrown 
                    throw new InvalidKeyException(Messages.getString(
                            "security.1A0", e)); //$NON-NLS-1$
                } 
            } 
 
            if (key instanceof DSAPublicKey) {
 
                DSAPublicKey publicKey = (DSAPublicKey) key;
                DSAParams params = publicKey.getParams();
 
                try { 
                    return engineGeneratePublic(new DSAPublicKeySpec(publicKey
                            .getY(), params.getP(), params.getQ(), params
                            .getG()));
                } catch (InvalidKeySpecException e) {
                    // Actually this exception shouldn't be thrown 
                    throw new InvalidKeyException(Messages.getString(
                            "security.1A1", e)); //$NON-NLS-1$
                }