List of utility methods to do Key Pair Create
void | generateKeyPair(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())); ... |
KeyPair | generateKeyPair(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(); |
KeyPair | generateKeys(int size) generate Keys KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(size); KeyPair kp = kpg.genKeyPair(); return kp; |
KeyManagerFactory | generateKeyStoreFromPEM(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); |