Mega Code Archive

 
Categories / Java / Security
 

Get the formats of the encoded bytes

import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; public class Main {   public static void main(String[] argv) throws Exception {     String algorithm = "DSA"; // or RSA, DH, etc.     // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair     KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);     keyGen.initialize(1024);     KeyPair keypair = keyGen.genKeyPair();     PrivateKey privateKey = keypair.getPrivate();     PublicKey publicKey = keypair.getPublic();     String format = privateKey.getFormat(); // PKCS#8     format = publicKey.getFormat(); // X.509   } }