Skip to content

Instantly share code, notes, and snippets.

@kabronkline
Last active November 13, 2018 02:39
Show Gist options
  • Select an option

  • Save kabronkline/599227ecb3acc352c94ee0d9fff7c8aa to your computer and use it in GitHub Desktop.

Select an option

Save kabronkline/599227ecb3acc352c94ee0d9fff7c8aa to your computer and use it in GitHub Desktop.
Generate EC Keypair in Java
package com.kabronkline.pkeydb;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
import java.util.Base64;
public class ECKeyPairGenerator {
public static void main(String[] args) throws InvalidAlgorithmParameterException, NoSuchProviderException, NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("EC","SunEC");
// 256 bit key with Koblitz curve
ECGenParameterSpec keyGenParamSpec = new ECGenParameterSpec("secp256k1");
keyPairGen.initialize(keyGenParamSpec);
KeyPair keyPair = keyPairGen.genKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println("Private Key: " + Base64.getEncoder().encodeToString(privateKey.getEncoded()));
System.out.println("Public Key: " + publicKey.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment