Code example for RSAPublicKeySpec

0
 
    @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());