public static PrivateKey load(InputStream is) throws CryptoException, IOException {
  byte[] streamContents=ReadUtil.readFully(is);
  EncryptionType encType=getEncryptionType(new ByteArrayInputStream(streamContents));
  if (encType == null) {
    throw new CryptoException(res.getString("NotValidPkcs8.exception.message"));
  }
  if (encType == ENCRYPTED) {
    throw new PrivateKeyEncryptedException(res.getString("Pkcs8IsEncrypted.exception.message"));
  }
  byte[] pvkBytes=null;
  PemInfo pemInfo=PemUtil.decode(new ByteArrayInputStream(streamContents));
  if (pemInfo != null) {
    pvkBytes=pemInfo.getContent();
  }
  if (pvkBytes == null) {
    pvkBytes=streamContents;
  }
  try {
    String privateKeyAlgorithm=getPrivateKeyAlgorithm(pvkBytes);
    PKCS8EncodedKeySpec privateKeySpec=new PKCS8EncodedKeySpec(pvkBytes);
    KeyFactory keyFactory=KeyFactory.getInstance(privateKeyAlgorithm);
    PrivateKey pvk=keyFactory.generatePrivate(privateKeySpec);
    return pvk;
  }
 catch (  NoSuchAlgorithmException ex) {
    throw new CryptoException(res.getString("NoLoadPkcs8PrivateKey.exception.message"),ex);
  }
catch (  InvalidKeySpecException ex) {
    throw new CryptoException(res.getString("NoLoadPkcs8PrivateKey.exception.message"),ex);
  }
}

public static PrivateKey load ( InputStream is ) throws CryptoException , IOException { byte [ ] streamContents = ReadUtil . readFully ( is ) ; EncryptionType encType = getEncryptionType ( new ByteArrayInputStream ( streamContents ) ) ; if ( encType == null ) { throw new CryptoException ( res . getString ( "NotValidPkcs8.exception.message" ) ) ; } if ( encType == ENCRYPTED ) { throw new PrivateKeyEncryptedException ( res . getString ( "Pkcs8IsEncrypted.exception.message" ) ) ; } byte [ ] pvkBytes = null ; PemInfo pemInfo = PemUtil . decode ( new ByteArrayInputStream ( streamContents ) ) ; if ( pemInfo != null ) { pvkBytes = pemInfo . getContent ( ) ; } if ( pvkBytes == null ) { pvkBytes = streamContents ; } try { String privateKeyAlgorithm = getPrivateKeyAlgorithm ( pvkBytes ) ; PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec ( pvkBytes ) ; KeyFactory keyFactory = KeyFactory . getInstance ( privateKeyAlgorithm ) ; PrivateKey pvk = keyFactory . generatePrivate ( privateKeySpec ) ; return pvk ; } catch ( NoSuchAlgorithmException ex ) { throw new CryptoException ( res . getString ( "NoLoadPkcs8PrivateKey.exception.message" ) , ex ) ; } catch ( InvalidKeySpecException ex ) { throw new CryptoException ( res . getString ( "NoLoadPkcs8PrivateKey.exception.message" ) , ex ) ; } }
load an unencrypted pkcs # 8 private key from the stream . the encoding of the private key may be pem or der .
