Last active
March 2, 2018 07:37
-
-
Save zhanggang807/c221bb0b61e58802fb8104d1b5f943c4 to your computer and use it in GitHub Desktop.
key generator of java security
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.KeyFactory; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.spec.DSAPrivateKeySpec; | |
import java.security.spec.InvalidKeySpecException; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.DESKeySpec; | |
public class KeyTest { | |
public static void main(String[] args) { | |
try { | |
generateKeyPair(); | |
generateKey(); | |
} catch (InvalidKeySpecException e) { | |
e.printStackTrace(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void generateKeyPair() throws NoSuchAlgorithmException, InvalidKeySpecException { | |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); | |
kpg.initialize(512); | |
KeyPair kp = kpg.generateKeyPair(); | |
System.out.println(kpg.getProvider()); | |
System.out.println(kpg.getAlgorithm()); | |
KeyFactory kf = KeyFactory.getInstance("DSA"); | |
DSAPrivateKeySpec dsaPKS = kf.getKeySpec(kp.getPrivate(), DSAPrivateKeySpec.class); | |
System.out.println("\tDSA param G:" + dsaPKS.getG()); | |
System.out.println("\tDSA param P:" + dsaPKS.getP()); | |
System.out.println("\tDSA param Q:" + dsaPKS.getQ()); | |
System.out.println("\tDSA param X:" + dsaPKS.getX()); | |
} | |
public static void generateKey() throws NoSuchAlgorithmException, InvalidKeySpecException { | |
KeyGenerator kg = KeyGenerator.getInstance("DES"); | |
SecretKey key = kg.generateKey(); | |
System.out.println(kg.getProvider()); | |
System.out.println(kg.getAlgorithm()); | |
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); | |
DESKeySpec desKS = (DESKeySpec) skf.getKeySpec(key, DESKeySpec.class); | |
System.out.println("\tDES key bytes size:" + desKS.getKey().length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment