Search in sources :

Example 11 with RSAKeyParameters

use of org.bouncycastle.crypto.params.RSAKeyParameters in project jruby-openssl by jruby.

the class PKCS10Request method generatePublicKey.

public PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    AsymmetricKeyParameter keyParams = PublicKeyFactory.createKey(publicKeyInfo);
    final KeySpec keySpec;
    final KeyFactory keyFactory;
    if (keyParams instanceof RSAKeyParameters) {
        RSAKeyParameters rsa = (RSAKeyParameters) keyParams;
        keySpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
        keyFactory = SecurityHelper.getKeyFactory("RSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof DSAPublicKeyParameters) {
        DSAPublicKeyParameters dsa = (DSAPublicKeyParameters) keyParams;
        DSAParameters params = dsa.getParameters();
        keySpec = new DSAPublicKeySpec(dsa.getY(), params.getP(), params.getQ(), params.getG());
        keyFactory = SecurityHelper.getKeyFactory("DSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof ECPublicKeyParameters) {
        ECPublicKeyParameters ec = (ECPublicKeyParameters) keyParams;
        ECDomainParameters ecParams = ec.getParameters();
        ECParameterSpec params = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN(), ecParams.getH(), ecParams.getSeed());
        // NOTE: likely to fail if non BC factory picked up :
        keySpec = new ECPublicKeySpec(ec.getQ(), params);
        keyFactory = SecurityHelper.getKeyFactory("EC");
        return keyFactory.generatePublic(keySpec);
    } else {
        throw new IllegalStateException("could not generate public key for request, params type: " + keyParams);
    }
}
Also used : DSAPublicKeyParameters(org.bouncycastle.crypto.params.DSAPublicKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) RSAKeyParameters(org.bouncycastle.crypto.params.RSAKeyParameters) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) PublicKeyFactory(org.bouncycastle.crypto.util.PublicKeyFactory) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

RSAKeyParameters (org.bouncycastle.crypto.params.RSAKeyParameters)11 BigInteger (java.math.BigInteger)6 RSAPublicKey (java.security.interfaces.RSAPublicKey)4 DEROctetString (org.bouncycastle.asn1.DEROctetString)4 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)4 DSAPublicKeyParameters (org.bouncycastle.crypto.params.DSAPublicKeyParameters)4 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)3 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)3 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)3 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)3 RSAPrivateCrtKeyParameters (org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)3 IOException (java.io.IOException)2 KeyFactory (java.security.KeyFactory)2 KeyPair (java.security.KeyPair)2 DSAPublicKey (java.security.interfaces.DSAPublicKey)2 KeySpec (java.security.spec.KeySpec)2 DHParameter (org.bouncycastle.asn1.pkcs.DHParameter)2 DSAParameter (org.bouncycastle.asn1.x509.DSAParameter)2 DHDomainParameters (org.bouncycastle.asn1.x9.DHDomainParameters)2