from PKCS8 to RSAPrivateKey - Java Security

Java examples for Security:Key

Description

from PKCS8 to RSAPrivateKey

Demo Code


//package com.java2s;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

public class Main {
    public static RSAPrivateKey fromPKCS8(byte[] pkcs8)
            throws InvalidKeyException, InvalidKeySpecException,
            NoSuchAlgorithmException {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8);
        PrivateKey key = keyFactory.generatePrivate(keySpec);

        if (key instanceof RSAPrivateKey) {
            return (RSAPrivateKey) key;
        } else {/*from  w  w w .j a va2  s.c  om*/
            throw new InvalidKeyException("Cannot cast " + key
                    + " to a RSA private key");
        }
    }
}

Related Tutorials