import org.apache.commons.codec.binary.Base64;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* @author 程英华
* @description 基于RSA非对称加密算法生成公私钥
* @date 2019-05-17
**/
public class Main {
public static void main(String[] args) {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048); // 指定位数
KeyPair keyPair = keyPairGen.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println(Base64.encodeBase64String(publicKey.getEncoded()));
System.out.println();
System.out.println(Base64.encodeBase64String(privateKey.getEncoded()));
}catch (Exception e) {
e.printStackTrace();
}
}
}