- Java Generate And Saving Keys Free
- Java Generate And Saving Keys Free
- Java Generate And Saving Keys Download
- Java Generate And Saving Keys Lyrics
gistfile1.java
How to use keystore in Java to store private key? Private keys must be encrypted when you store them on disk. Do not use it as is. Java: How can I generate. Java, PKCS12, keystore, tutorial.PKCS12 is an active file format for storing cryptography objects as a single file. It can be used to store secret key, private key and certificate.It is a standardized format published by RSA LaboratoPixelstech, this page is to provide vistors information of the most updated technology information around the world.
Java Generate And Saving Keys Free
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); |
} |
} |
commented Mar 14, 2018
Java Generate And Saving Keys Free
Java Generate And Saving Keys Download
Java Generate And Saving Keys Lyrics
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment