Search in sources :

Example 11 with KeyPairGenerator

use of java.security.KeyPairGenerator in project neo4j by neo4j.

the class Certificates method createSelfSignedCertificate.

public void createSelfSignedCertificate(File certificatePath, File privateKeyPath, String hostName) throws GeneralSecurityException, IOException, OperatorCreationException {
    installCleanupHook(certificatePath, privateKeyPath);
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(DEFAULT_ENCRYPTION);
    keyGen.initialize(2048, random);
    KeyPair keypair = keyGen.generateKeyPair();
    // Prepare the information required for generating an X.509 certificate.
    X500Name owner = new X500Name("CN=" + hostName);
    X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(owner, new BigInteger(64, random), NOT_BEFORE, NOT_AFTER, owner, keypair.getPublic());
    PrivateKey privateKey = keypair.getPrivate();
    ContentSigner signer = new JcaContentSignerBuilder("SHA512WithRSAEncryption").build(privateKey);
    X509CertificateHolder certHolder = builder.build(signer);
    X509Certificate cert = new JcaX509CertificateConverter().setProvider(PROVIDER).getCertificate(certHolder);
    //check so that cert is valid
    cert.verify(keypair.getPublic());
    //write to disk
    writePem("CERTIFICATE", cert.getEncoded(), certificatePath);
    writePem("PRIVATE KEY", privateKey.getEncoded(), privateKeyPath);
    // Mark as done so we don't clean up certificates
    cleanupRequired = false;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) X509v3CertificateBuilder(org.bouncycastle.cert.X509v3CertificateBuilder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) ContentSigner(org.bouncycastle.operator.ContentSigner) BigInteger(java.math.BigInteger) KeyPairGenerator(java.security.KeyPairGenerator) X500Name(org.bouncycastle.asn1.x500.X500Name) X509Certificate(java.security.cert.X509Certificate)

Example 12 with KeyPairGenerator

use of java.security.KeyPairGenerator in project platformlayer by platformlayer.

the class CryptoUtils method generateKeyPair.

public static KeyPair generateKeyPair(String algorithm, int keysize) {
    KeyPairGenerator generator;
    try {
        generator = KeyPairGenerator.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Error loading crypto provider", e);
    }
    generator.initialize(keysize);
    KeyPair keyPair = generator.generateKeyPair();
    return keyPair;
}
Also used : KeyPair(java.security.KeyPair) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 13 with KeyPairGenerator

use of java.security.KeyPairGenerator 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 14 with KeyPairGenerator

use of java.security.KeyPairGenerator in project robovm by robovm.

the class AlgorithmParameterKeyAgreementHelper method test.

@Override
public void test(AlgorithmParameters parameters) {
    KeyPairGenerator generator = null;
    try {
        generator = KeyPairGenerator.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    generator.initialize(1024);
    KeyPair keyPair = generator.generateKeyPair();
    KeyAgreement keyAgreement = null;
    try {
        keyAgreement = KeyAgreement.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    try {
        keyAgreement.init(keyPair.getPrivate());
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    }
    try {
        keyAgreement.doPhase(keyPair.getPublic(), true);
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    } catch (IllegalStateException e) {
        Assert.fail(e.getMessage());
    }
    Assert.assertNotNull("generated secret is null", keyAgreement.generateSecret());
}
Also used : KeyPair(java.security.KeyPair) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException)

Example 15 with KeyPairGenerator

use of java.security.KeyPairGenerator in project robovm by robovm.

the class AlgorithmParameterSignatureHelper method test.

@Override
public void test(AlgorithmParameters parameters) {
    Signature signature = null;
    try {
        signature = Signature.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    T parameterSpec = null;
    try {
        parameterSpec = parameters.getParameterSpec(parameterSpecClass);
    } catch (InvalidParameterSpecException e) {
        Assert.fail(e.getMessage());
    }
    KeyPairGenerator generator = null;
    try {
        generator = KeyPairGenerator.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    try {
        generator.initialize(parameterSpec);
    } catch (InvalidAlgorithmParameterException e) {
        Assert.fail(e.getMessage());
    }
    KeyPair keyPair = generator.genKeyPair();
    try {
        signature.initSign(keyPair.getPrivate());
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    }
    try {
        signature.update(plainData.getBytes());
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
    byte[] signed = null;
    try {
        signed = signature.sign();
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
    try {
        signature.initVerify(keyPair.getPublic());
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    }
    try {
        signature.update(plainData.getBytes());
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
    try {
        Assert.assertTrue("signature could not be verified", signature.verify(signed));
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : KeyPair(java.security.KeyPair) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) KeyPairGenerator(java.security.KeyPairGenerator) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

KeyPairGenerator (java.security.KeyPairGenerator)197 KeyPair (java.security.KeyPair)145 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)43 SecureRandom (java.security.SecureRandom)39 PublicKey (java.security.PublicKey)27 PrivateKey (java.security.PrivateKey)26 X509Certificate (java.security.cert.X509Certificate)23 KeyFactory (java.security.KeyFactory)21 IOException (java.io.IOException)19 BigInteger (java.math.BigInteger)17 GeneralSecurityException (java.security.GeneralSecurityException)15 Signature (java.security.Signature)15 Date (java.util.Date)15 Cipher (javax.crypto.Cipher)15 KeyAgreement (javax.crypto.KeyAgreement)15 RSAPublicKey (java.security.interfaces.RSAPublicKey)14 X500Principal (javax.security.auth.x500.X500Principal)13 ECPrivateKey (java.security.interfaces.ECPrivateKey)12 ECPublicKey (java.security.interfaces.ECPublicKey)12 HashMap (java.util.HashMap)11