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;
}
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;
}
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;
}
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());
}
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());
}
}
Aggregations