Code example for Key

Methods: getClass, getEncoded, getFormat

0
            try { 
                return engineGeneratePrivate(new DSAPrivateKeySpec(x, p, q, g));
            } catch (InvalidKeySpecException e) {
                throw new InvalidKeyException(e);
            } 
        } else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
            byte[] encoded = key.getEncoded();
            if (encoded == null) {
                throw new InvalidKeyException("Key does not support encoding");
            } 
            try { 
                return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
            } catch (InvalidKeySpecException e) {
                throw new InvalidKeyException(e);
            } 
        } else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
            byte[] encoded = key.getEncoded();
            if (encoded == null) {
                throw new InvalidKeyException("Key does not support encoding");
            } 
            try { 
                return engineGeneratePublic(new X509EncodedKeySpec(encoded));
            } catch (InvalidKeySpecException e) {
                throw new InvalidKeyException(e);
            } 
        } else { 
            throw new InvalidKeyException("Key must be DSA public or private key; was "
                    + key.getClass().getName());
        } 
    } 
}