Code example for Key

Methods: getAlgorithm, getClass, getEncoded, getFormat

0
                throw new InvalidKeySpecException(e);
            } 
        } 
        throw new InvalidKeySpecException("Must use RSAPublicKeySpec or PKCS8EncodedKeySpec; was "
                + keySpec.getClass().getName());
    } 
 
    @Override 
    protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
            throws InvalidKeySpecException { 
        if (key == null) {
            throw new InvalidKeySpecException("key == null");
        } 
 
        if (keySpec == null) {
            throw new InvalidKeySpecException("keySpec == null");
        } 
 
        if (key instanceof RSAPublicKey) {
            RSAPublicKey rsaKey = (RSAPublicKey) key;
 
            if (RSAPublicKeySpec.class.equals(keySpec)) {
                BigInteger modulus = rsaKey.getModulus();
                BigInteger publicExponent = rsaKey.getPublicExponent();
                return (T) new RSAPublicKeySpec(modulus, publicExponent);
            } else if (X509EncodedKeySpec.class.equals(keySpec)) {
                return (T) new X509EncodedKeySpec(key.getEncoded());
            } else { 
                throw new InvalidKeySpecException("Must be RSAPublicKeySpec or X509EncodedKeySpec");
            } 
        } else if (key instanceof RSAPrivateCrtKey) {
            RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
 
            if (RSAPrivateKeySpec.class.equals(keySpec)) {
                BigInteger modulus = rsaKey.getModulus();
                BigInteger privateExponent = rsaKey.getPrivateExponent();
                return (T) new RSAPrivateKeySpec(modulus, privateExponent);
            } else if (RSAPrivateCrtKeySpec.class.equals(keySpec)) {
                BigInteger modulus = rsaKey.getModulus();
                BigInteger publicExponent = rsaKey.getPublicExponent();
                BigInteger privateExponent = rsaKey.getPrivateExponent();
                BigInteger primeP = rsaKey.getPrimeP();
                BigInteger primeQ = rsaKey.getPrimeQ();
                BigInteger primeExponentP = rsaKey.getPrimeExponentP();
                BigInteger primeExponentQ = rsaKey.getPrimeExponentQ();
                BigInteger crtCoefficient = rsaKey.getCrtCoefficient();
                return (T) new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent,
                        primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient);
            } else if (PKCS8EncodedKeySpec.class.equals(keySpec)) {
                return (T) new PKCS8EncodedKeySpec(rsaKey.getEncoded());
            } else { 
                throw new InvalidKeySpecException(
                        "Must be RSAPrivateKeySpec or or RSAPrivateCrtKeySpec or PKCS8EncodedKeySpec"); 
            } 
        } else if (key instanceof RSAPrivateKey) {
            RSAPrivateKey rsaKey = (RSAPrivateKey) key;
 
            if (RSAPrivateKeySpec.class.equals(keySpec)) {
                BigInteger modulus = rsaKey.getModulus();
                BigInteger privateExponent = rsaKey.getPrivateExponent();
                return (T) new RSAPrivateKeySpec(modulus, privateExponent);
            } else if (RSAPrivateCrtKeySpec.class.equals(keySpec)) {
                BigInteger modulus = rsaKey.getModulus();
                BigInteger privateExponent = rsaKey.getPrivateExponent();
                return (T) new RSAPrivateCrtKeySpec(modulus, null, privateExponent, null, null,
                        null, null, null); 
            } else if (PKCS8EncodedKeySpec.class.equals(keySpec)) {
                return (T) new PKCS8EncodedKeySpec(rsaKey.getEncoded());
            } else { 
                throw new InvalidKeySpecException(
                        "Must be RSAPrivateKeySpec or PKCS8EncodedKeySpec"); 
            } 
        } else { 
            throw new InvalidKeySpecException("Must be RSAPublicKey or RSAPrivateKey");
        } 
    } 
 
    @Override 
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
        if (key == null) {
            throw new InvalidKeyException("key == null");
        } 
 
        if (key instanceof RSAPublicKey) {
            RSAPublicKey rsaKey = (RSAPublicKey) key;
 
            try {