数字证书密钥对生成
package com.test;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
public class KeyPairTest {
public static KeyPair getRSAKeyPair(int length) throws NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(length);
KeyPair keyPair = kpg.genKeyPair();
return keyPair;
}
public static KeyPair getDSAKeyPair(int length) throws NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
kpg.initialize(length);
KeyPair keyPair = kpg.genKeyPair();
return keyPair;
}
public static void main(String[]args) throws NoSuchAlgorithmException {
KeyPair keyPair = KeyPairTest.getRSAKeyPair(1024);
System.out.println(keyPair.getPublic().getAlgorithm());
keyPair = KeyPairTest.getDSAKeyPair(1024);
System.out.println(keyPair.getPublic().getAlgorithm());
}
}