Java Utililty Methods Key Pair Create

List of utility methods to do Key Pair Create

Description

The list of methods to do Key Pair Create are organized into topic(s).

Method

voidgenerateKeyPair(String keyAlgorithm, int keySize)
generate Key Pair
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgorithm); 
kpg.initialize(keySize); 
KeyPair kp = kpg.generateKeyPair();
System.out.println("-----BEGIN PRIVATE KEY-----");
System.out.println(Base64.getMimeEncoder().encodeToString(kp.getPrivate().getEncoded()));
System.out.println("-----END PRIVATE KEY-----");
System.out.println("-----BEGIN PUBLIC KEY-----");
System.out.println(Base64.getMimeEncoder().encodeToString(kp.getPublic().getEncoded()));
...
KeyPairgenerateKeyPair(String type, int size)
generate Key Pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(type);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(size, random);
random.setSeed(System.currentTimeMillis());
return keyGen.generateKeyPair();
KeyPairgenerateKeys(int size)
generate Keys
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(size);
KeyPair kp = kpg.genKeyPair();
return kp;
KeyManagerFactorygenerateKeyStoreFromPEM(File certFile, File keyFile)
Creates a KeyManagerFactory from PEM files (something java should just do...).
char[] password = UUID.randomUUID().toString().toCharArray();
try {
    List<X509Certificate> certs = getPEMFileCerts(certFile);
    RSAPrivateKey key = getPEMFileKey(keyFile);
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(null);
    for (int i = 0; i < certs.size(); i++) {
        keystore.setCertificateEntry("cert-" + i, certs.get(i));
...
byte[]generateKeyZ(ECPrivateKey privateKey, ECPublicKey publicKey)
generate Key Z
try {
    KeyAgreement ka = KeyAgreement.getInstance("ECDH");
    ka.init(privateKey);
    ka.doPhase(publicKey, true);
    return ka.generateSecret();
} catch (Exception ex) {
    throw new SecurityException(ex);