Code example for RSAPrivateKeySpec

0
        } 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");
Stop searching for code, let great code find you!  Add Codota to your java IDE