Skip to content

Instantly share code, notes, and snippets.

@liudong
Created November 1, 2012 13:49
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save liudong/3993726 to your computer and use it in GitHub Desktop.
Save liudong/3993726 to your computer and use it in GitHub Desktop.
Java: Generate Public/Private Keys
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.KeyFactory;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.NoSuchAlgorithmException;
import com.sun.jersey.core.util.Base64;
public class GeneratePublicPrivateKeys {
private static void generateKeys(String keyAlgorithm, int numBits) {
try {
// Get the public/private key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
keyGen.initialize(numBits);
KeyPair keyPair = keyGen.genKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println("\n" + "Generating key/value pair using " + privateKey.getAlgorithm() + " algorithm");
// Get the bytes of the public and private keys
byte[] privateKeyBytes = privateKey.getEncoded();
byte[] publicKeyBytes = publicKey.getEncoded();
// Get the formats of the encoded bytes
String formatPrivate = privateKey.getFormat(); // PKCS#8
String formatPublic = publicKey.getFormat(); // X.509
System.out.println("Private Key : " + Base64.encode(String.valueOf(privateKeyBytes)));
System.out.println("Public Key : " + Base64.encode(String.valueOf(publicKeyBytes)));
// The bytes can be converted back to public and private key objects
KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);
// The original and new keys are the same
System.out.println(" Are both private keys equal? " + privateKey.equals(privateKey2));
System.out.println(" Are both public keys equal? " + publicKey.equals(publicKey2));
} catch (InvalidKeySpecException specException) {
System.out.println("Exception");
System.out.println("Invalid Key Spec Exception");
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception");
System.out.println("No such algorithm: " + keyAlgorithm);
}
}
public static void main(String[] args) {
// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
generateKeys("DSA", 1024);
// Generate a 576-bit DH key pair
generateKeys("DH", 576);
// Generate a 1024-bit RSA key pair
generateKeys("RSA", 1024);
}
}
@nissshh
Copy link

nissshh commented Mar 14, 2018

Hi

You post is interestnig , is there away I can create a privatre key instance via a signature given stiring?

I have pub/private keys generated already

KeyPairGenerator keyPairGenerator is going to createa key pair, but in my case I alrady have it and then further want to use them for signign.

e.g

//ecdsaSign.initSign(keyPair.getPrivate());
byte[] pkInfo = "51114cac71a9575bc1b39104d176a39d81bd1a705b9a1ad32efd2222f13e59ad".getBytes();
// PrivateKey pvtKey = DSAPrivateKey <<<<< create a private key here via above string. instead of keyPair created above.
ecdsaSign.initSign(pvtKey);
//byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
byte[] publicKeyBytes = "025fe2d166a5a8ff005eb0c799a474174f5d061de266438c69d36c2032c6bff51a".getBytes();

@abhishekprasad870
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment