Public Private Key Generator Java
importjava.security.KeyPairGenerator; |
importjava.security.KeyPair; |
importjava.security.PrivateKey; |
importjava.security.PublicKey; |
importjava.security.KeyFactory; |
importjava.security.spec.EncodedKeySpec; |
importjava.security.spec.PKCS8EncodedKeySpec; |
importjava.security.spec.X509EncodedKeySpec; |
importjava.security.spec.InvalidKeySpecException; |
importjava.security.NoSuchAlgorithmException; |
importcom.sun.jersey.core.util.Base64; |
publicclassGeneratePublicPrivateKeys { |
privatestaticvoidgenerateKeys(StringkeyAlgorithm, intnumBits) { |
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 =newPKCS8EncodedKeySpec(privateKeyBytes); |
PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec); |
EncodedKeySpec publicKeySpec =newX509EncodedKeySpec(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); |
} |
} |
publicstaticvoidmain(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); |
} |
} |
/rsa-generate-public-and-private-key.html. The Java Keytool is a command line tool which can generate public key / private key pairs and store them in a Java KeyStore.The Keytool executable is distributed with the Java SDK (or JRE), so if you have an SDK installed you will also have the Keytool executable.
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()); |
Public Private Key Generator Java Code
Public Vs Private Class Java
- I'm looking for a java library or code to generate certificates, public and private keys on the fly without to use third party programs (such as openssl). I think something that is doeing keytool+openssl but from Java code. Consider a java servlet based web application secured with ssl and client authentification.
- A Key pair generator for a particular algorithm creates a public/private key pair that can be used with this algorithm. It also associates algorithm-specific parameters with each of the generated keys. There are two ways to generate a key pair: in an algorithm-independent manner, and in an algorithm-specific manner.