use of org.bouncycastle.jce.spec.ECParameterSpec 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);
}
}
use of org.bouncycastle.jce.spec.ECParameterSpec in project karaf by apache.
the class KeyPairLoader method convertPrivateToPublicKey.
private static PublicKey convertPrivateToPublicKey(PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
if (privateKey instanceof RSAPrivateCrtKey) {
KeySpec keySpec = new RSAPublicKeySpec(((RSAPrivateCrtKey) privateKey).getModulus(), ((RSAPrivateCrtKey) privateKey).getPublicExponent());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
} else if (privateKey instanceof ECPrivateKey) {
ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
// Derive the public point by multiplying the generator by the private value
ECParameterSpec paramSpec = EC5Util.convertSpec(ecPrivateKey.getParams());
ECPoint q = paramSpec.getG().multiply(ecPrivateKey.getS());
KeySpec keySpec = new ECPublicKeySpec(q, paramSpec);
KeyFactory keyFactory = KeyFactory.getInstance("EC");
return keyFactory.generatePublic(keySpec);
} else if (privateKey instanceof DSAPrivateKey) {
DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
BigInteger q = dsaPrivateKey.getParams().getQ();
BigInteger p = dsaPrivateKey.getParams().getP();
KeySpec keySpec = new DSAPublicKeySpec(q.modPow(dsaPrivateKey.getX(), p), p, q, dsaPrivateKey.getParams().getG());
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return keyFactory.generatePublic(keySpec);
} else {
LOGGER.warn("Unable to convert private key to public key. Only RSA, DSA + ECDSA supported");
return null;
}
}
Aggregations