core/src/main/java/org/bouncycastle/crypto/util/PrivateKeyFactory.java
/**
     * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
     * 
     * @param keyInfo the PrivateKeyInfo object containing the key material
     * @return a suitable private key parameter
     * @throws IOException on an error decoding the key
     */
Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.

@param keyInfo the PrivateKeyInfo object containing the key material
@return a suitable private key parameter
@throws IOException on an error decoding the key
Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object . @param keyInfo the PrivateKeyInfo object containing the key material @return a suitable private key parameter @throws IOException on an error decoding the key
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException
    {
        AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
        ASN1ObjectIdentifier algOID = algId.getAlgorithm();
        if (algOID.equals(PKCSObjectIdentifiers.rsaEncryption))
        {
            RSAPrivateKey keyStructure = RSAPrivateKey.getInstance(keyInfo.parsePrivateKey());
            return new RSAPrivateCrtKeyParameters(keyStructure.getModulus(),
                keyStructure.getPublicExponent(), keyStructure.getPrivateExponent(),
                keyStructure.getPrime1(), keyStructure.getPrime2(), keyStructure.getExponent1(),
                keyStructure.getExponent2(), keyStructure.getCoefficient());
        }
        else if (algOID.equals(PKCSObjectIdentifiers.dhKeyAgreement))
        {
            DHParameter params = DHParameter.getInstance(algId.getParameters());
            ASN1Integer derX = (ASN1Integer)keyInfo.parsePrivateKey();
            BigInteger lVal = params.getL();
            int l = lVal == null ? 0 : lVal.intValue();
            DHParameters dhParams = new DHParameters(params.getP(), params.getG(), null, l);
            return new DHPrivateKeyParameters(derX.getValue(), dhParams);
        }
        else if (algOID.equals(OIWObjectIdentifiers.elGamalAlgorithm))
        {
            ElGamalParameter params = ElGamalParameter.getInstance(algId.getParameters());
            ASN1Integer derX = (ASN1Integer)keyInfo.parsePrivateKey();
            return new ElGamalPrivateKeyParameters(derX.getValue(), new ElGamalParameters(
                params.getP(), params.getG()));
        }
        else if (algOID.equals(X9ObjectIdentifiers.id_dsa))
        {
            ASN1Integer derX = (ASN1Integer)keyInfo.parsePrivateKey();
            ASN1Encodable de = algId.getParameters();
            DSAParameters parameters = null;
            if (de != null)
            {
                DSAParameter params = DSAParameter.getInstance(de.toASN1Primitive());
                parameters = new DSAParameters(params.getP(), params.getQ(), params.getG());
            }
            return new DSAPrivateKeyParameters(derX.getValue(), parameters);
        }
        else if (algOID.equals(X9ObjectIdentifiers.id_ecPublicKey))
        {
            X962Parameters params = new X962Parameters((ASN1Primitive)algId.getParameters());
            X9ECParameters x9;
            ECDomainParameters dParams;
            if (params.isNamedCurve())
            {
                ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)params.getParameters();
                x9 = CustomNamedCurves.getByOID(oid);
                if (x9 == null)
                {
                    x9 = ECNamedCurveTable.getByOID(oid);
                }
                dParams = new ECNamedDomainParameters(
                    oid, x9.getCurve(), x9.getG(), x9.getN(), x9.getH(), x9.getSeed());
            }
            else
            {
                x9 = X9ECParameters.getInstance(params.getParameters());
                dParams = new ECDomainParameters(
                    x9.getCurve(), x9.getG(), x9.getN(), x9.getH(), x9.getSeed());
            }
            ECPrivateKey ec = ECPrivateKey.getInstance(keyInfo.parsePrivateKey());
            BigInteger d = ec.getKey();
            return new ECPrivateKeyParameters(d, dParams);
        }
        else if (algOID.equals(EdECObjectIdentifiers.id_X25519))
        {
            return new X25519PrivateKeyParameters(getRawKey(keyInfo, X25519PrivateKeyParameters.KEY_SIZE), 0);
        }
        else if (algOID.equals(EdECObjectIdentifiers.id_X448))
        {
            return new X448PrivateKeyParameters(getRawKey(keyInfo, X448PrivateKeyParameters.KEY_SIZE), 0);
        }
        else if (algOID.equals(EdECObjectIdentifiers.id_Ed25519))
        {
            return new Ed25519PrivateKeyParameters(getRawKey(keyInfo, Ed25519PrivateKeyParameters.KEY_SIZE), 0);
        }
        else if (algOID.equals(EdECObjectIdentifiers.id_Ed448))
        {
            return new Ed448PrivateKeyParameters(getRawKey(keyInfo, Ed448PrivateKeyParameters.KEY_SIZE), 0);
        }
        else
        {
            throw new RuntimeException("algorithm identifier in private key not recognised");
        }
    }
public static AsymmetricKeyParameter createKey ( PrivateKeyInfo keyInfo ) throws IOException { AlgorithmIdentifier algId = keyInfo . getPrivateKeyAlgorithm ( ) ; ASN1ObjectIdentifier algOID = algId . getAlgorithm ( ) ; if ( algOID . equals ( PKCSObjectIdentifiers . rsaEncryption ) ) { RSAPrivateKey keyStructure = RSAPrivateKey . getInstance ( keyInfo . parsePrivateKey ( ) ) ; return new RSAPrivateCrtKeyParameters ( keyStructure . getModulus ( ) , keyStructure . getPublicExponent ( ) , keyStructure . getPrivateExponent ( ) , keyStructure . getPrime1 ( ) , keyStructure . getPrime2 ( ) , keyStructure . getExponent1 ( ) , keyStructure . getExponent2 ( ) , keyStructure . getCoefficient ( ) ) ; } else if ( algOID . equals ( PKCSObjectIdentifiers . dhKeyAgreement ) ) { DHParameter params = DHParameter . getInstance ( algId . getParameters ( ) ) ; ASN1Integer derX = ( ASN1Integer ) keyInfo . parsePrivateKey ( ) ; BigInteger lVal = params . getL ( ) ; int l = lVal == null ? 0 : lVal . intValue ( ) ; DHParameters dhParams = new DHParameters ( params . getP ( ) , params . getG ( ) , null , l ) ; return new DHPrivateKeyParameters ( derX . getValue ( ) , dhParams ) ; } else if ( algOID . equals ( OIWObjectIdentifiers . elGamalAlgorithm ) ) { ElGamalParameter params = ElGamalParameter . getInstance ( algId . getParameters ( ) ) ; ASN1Integer derX = ( ASN1Integer ) keyInfo . parsePrivateKey ( ) ; return new ElGamalPrivateKeyParameters ( derX . getValue ( ) , new ElGamalParameters ( params . getP ( ) , params . getG ( ) ) ) ; } else if ( algOID . equals ( X9ObjectIdentifiers . id_dsa ) ) { ASN1Integer derX = ( ASN1Integer ) keyInfo . parsePrivateKey ( ) ; ASN1Encodable de = algId . getParameters ( ) ; DSAParameters parameters = null ; if ( de != null ) { DSAParameter params = DSAParameter . getInstance ( de . toASN1Primitive ( ) ) ; parameters = new DSAParameters ( params . getP ( ) , params . getQ ( ) , params . getG ( ) ) ; } return new DSAPrivateKeyParameters ( derX . getValue ( ) , parameters ) ; } else if ( algOID . equals ( X9ObjectIdentifiers . id_ecPublicKey ) ) { X962Parameters params = new X962Parameters ( ( ASN1Primitive ) algId . getParameters ( ) ) ; X9ECParameters x9 ; ECDomainParameters dParams ; if ( params . isNamedCurve ( ) ) { ASN1ObjectIdentifier oid = ( ASN1ObjectIdentifier ) params . getParameters ( ) ; x9 = CustomNamedCurves . getByOID ( oid ) ; if ( x9 == null ) { x9 = ECNamedCurveTable . getByOID ( oid ) ; } dParams = new ECNamedDomainParameters ( oid , x9 . getCurve ( ) , x9 . getG ( ) , x9 . getN ( ) , x9 . getH ( ) , x9 . getSeed ( ) ) ; } else { x9 = X9ECParameters . getInstance ( params . getParameters ( ) ) ; dParams = new ECDomainParameters ( x9 . getCurve ( ) , x9 . getG ( ) , x9 . getN ( ) , x9 . getH ( ) , x9 . getSeed ( ) ) ; } ECPrivateKey ec = ECPrivateKey . getInstance ( keyInfo . parsePrivateKey ( ) ) ; BigInteger d = ec . getKey ( ) ; return new ECPrivateKeyParameters ( d , dParams ) ; } else if ( algOID . equals ( EdECObjectIdentifiers . id_X25519 ) ) { return new X25519PrivateKeyParameters ( getRawKey ( keyInfo , X25519PrivateKeyParameters . KEY_SIZE ) , 0 ) ; } else if ( algOID . equals ( EdECObjectIdentifiers . id_X448 ) ) { return new X448PrivateKeyParameters ( getRawKey ( keyInfo , X448PrivateKeyParameters . KEY_SIZE ) , 0 ) ; } else if ( algOID . equals ( EdECObjectIdentifiers . id_Ed25519 ) ) { return new Ed25519PrivateKeyParameters ( getRawKey ( keyInfo , Ed25519PrivateKeyParameters . KEY_SIZE ) , 0 ) ; } else if ( algOID . equals ( EdECObjectIdentifiers . id_Ed448 ) ) { return new Ed448PrivateKeyParameters ( getRawKey ( keyInfo , Ed448PrivateKeyParameters . KEY_SIZE ) , 0 ) ; } else { throw new RuntimeException ( "algorithm identifier in private key not recognised" ) ; } }
Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object .
// TODO? //      else if (algOID.equals(X9ObjectIdentifiers.dhpublicnumber))