Search in sources :

Example 11 with DSAParameterSpec

use of java.security.spec.DSAParameterSpec in project robovm by robovm.

the class RandomPrivateKeyX509ExtendedKeyManager method getPrivateKey.

@Override
public PrivateKey getPrivateKey(String alias) {
    PrivateKey originalPrivateKey = super.getPrivateKey(alias);
    if (originalPrivateKey == null) {
        return null;
    }
    PrivateKey result;
    String keyAlgorithm = originalPrivateKey.getAlgorithm();
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        if ("RSA".equals(keyAlgorithm)) {
            RSAPrivateKeySpec originalKeySpec = keyFactory.getKeySpec(originalPrivateKey, RSAPrivateKeySpec.class);
            int keyLengthBits = originalKeySpec.getModulus().bitLength();
            // Use a cache because RSA key generation is slow.
            String cacheKey = keyAlgorithm + "-" + keyLengthBits;
            result = cachedKeys.get(cacheKey);
            if (result == null) {
                KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
                keyPairGenerator.initialize(keyLengthBits);
                result = keyPairGenerator.generateKeyPair().getPrivate();
                cachedKeys.put(cacheKey, result);
            }
        } else if ("DSA".equals(keyAlgorithm)) {
            DSAPrivateKeySpec originalKeySpec = keyFactory.getKeySpec(originalPrivateKey, DSAPrivateKeySpec.class);
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
            keyPairGenerator.initialize(new DSAParameterSpec(originalKeySpec.getP(), originalKeySpec.getQ(), originalKeySpec.getG()));
            result = keyPairGenerator.generateKeyPair().getPrivate();
        } else {
            Assert.fail("Unsupported key algorithm: " + originalPrivateKey.getAlgorithm());
            result = null;
        }
    } catch (GeneralSecurityException e) {
        Assert.fail("Failed to generate private key: " + e);
        result = null;
    }
    return result;
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAParameterSpec(java.security.spec.DSAParameterSpec) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Example 12 with DSAParameterSpec

use of java.security.spec.DSAParameterSpec in project robovm by robovm.

the class JDKDSAPrivateKey method readObject.

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    this.x = (BigInteger) in.readObject();
    this.dsaSpec = new DSAParameterSpec((BigInteger) in.readObject(), (BigInteger) in.readObject(), (BigInteger) in.readObject());
    this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
    attrCarrier.readObject(in);
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) PKCS12BagAttributeCarrierImpl(org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl) BigInteger(java.math.BigInteger)

Example 13 with DSAParameterSpec

use of java.security.spec.DSAParameterSpec in project robovm by robovm.

the class DSAPublicKeyImpl method readObject.

private void readObject(java.io.ObjectInputStream in) throws NotActiveException, IOException, ClassNotFoundException {
    in.defaultReadObject();
    params = new DSAParameterSpec(p, q, g);
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec)

Example 14 with DSAParameterSpec

use of java.security.spec.DSAParameterSpec in project jdk8u_jdk by JetBrains.

the class DSAKeyPairGenerator method initialize.

/**
     * Initializes the DSA object using a DSA parameter object.
     *
     * @param params a fully initialized DSA parameter object.
     */
public void initialize(DSAParams params, SecureRandom random) {
    if (params == null) {
        throw new InvalidParameterException("Params must not be null");
    }
    DSAParameterSpec spec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
    initialize0(spec, random);
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec)

Example 15 with DSAParameterSpec

use of java.security.spec.DSAParameterSpec in project jdk8u_jdk by JetBrains.

the class DSAParameterGenerator method engineGenerateParameters.

/**
     * Generates the parameters.
     *
     * @return the new AlgorithmParameters object
     */
protected AlgorithmParameters engineGenerateParameters() {
    AlgorithmParameters algParams = null;
    try {
        if (this.random == null) {
            this.random = new SecureRandom();
        }
        if (valueL == -1) {
            try {
                engineInit(DEFAULTS, this.random);
            } catch (InvalidAlgorithmParameterException iape) {
            // should never happen
            }
        }
        BigInteger[] pAndQ = generatePandQ(this.random, valueL, valueN, seedLen);
        BigInteger paramP = pAndQ[0];
        BigInteger paramQ = pAndQ[1];
        BigInteger paramG = generateG(paramP, paramQ);
        DSAParameterSpec dsaParamSpec = new DSAParameterSpec(paramP, paramQ, paramG);
        algParams = AlgorithmParameters.getInstance("DSA", "SUN");
        algParams.init(dsaParamSpec);
    } catch (InvalidParameterSpecException e) {
        // this should never happen
        throw new RuntimeException(e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        // this should never happen, because we provide it
        throw new RuntimeException(e.getMessage());
    } catch (NoSuchProviderException e) {
        // this should never happen, because we provide it
        throw new RuntimeException(e.getMessage());
    }
    return algParams;
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

DSAParameterSpec (java.security.spec.DSAParameterSpec)56 BigInteger (java.math.BigInteger)20 DSAParams (java.security.interfaces.DSAParams)16 KeyPairGenerator (java.security.KeyPairGenerator)12 SecureRandom (java.security.SecureRandom)10 AlgorithmParameters (java.security.AlgorithmParameters)8 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)6 DSAPublicKey (java.security.interfaces.DSAPublicKey)6 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)6 KeyPair (java.security.KeyPair)5 InvalidParameterException (java.security.InvalidParameterException)4 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)3 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)3 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)2 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)2 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)2 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)2