Java generates SM2 certificate based on bouncy castle (CER)

Keywords: Java

Java generates SM2 certificate based on bouncy castle (CER)

You can add QQ 783021975 to consult with related questions. Part of the code will be updated later

Sorting out. It's generated by calling BouncCastle in Java code. First, let's see that the signature algorithm and the public key parameters are in line with the SM2 algorithm

You can add QQ 783021975 to consult with related questions.

10:37:14 on August 17, 2017, the code for generating cer certificate was added

     * National secret certificate signature algorithm identification
    private static String  SignAlgor = "";
	 * Method of generating national secret ROOT certificate
	 * @param pageCert.getCn()+","+
	 * @throws Exception
	public static void genSM2CertByRoot() throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		org.bouncycastle.jce.provider.BouncyCastleProvider bouncyCastleProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
		//Name of the certificate
                String fileName = "root"+new Date().getTime()/1000;
		String path  = "Saved path";
		String rootCertPath = path+fileName+".cer";
		try {
			KeyPair kp = KeyGenUtil.getKeyPair2SM2(path,fileName);//This is to generate the SM2 public private key pair
			System.out.println("=====Public key algorithm====="+kp.getPublic().getAlgorithm());
			BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) kp.getPrivate();//You can use ecprivatekey privatekey
			BCECPublicKey bcecPublicKey = (BCECPublicKey) kp.getPublic();//You can use ecpublickey publickey
			//I get the passed parameters through the web page to apply for the server certificate information. If the test is written to death. There is nothing in this step.
			X500Principal principal = new X500Principal("CN=Xiaoshuai, blog,O=Xiaoshuai, blog");
			//X500Principal principal = new X500Principal("CN="+pageCert.getCn()+",O="+pageCert.getO());
			X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
			certGen.setIssuerDN(principal);certGen.setNotBefore(new Date());
			X509Certificate rootCert = certGen.generateX509Certificate(bcecPrivateKey, "BC");
			FileOutputStream outputStream = new FileOutputStream(rootCertPath);
			} catch (Exception e) {"======Root certificate request failed"+e.getMessage());return null;
} Purchase link. Voluntary purchase


Project support

1. Generate RSA algorithm cer certificate

2.SM2 algorithm cer certificate.

3. Generate root certificate. Child certificate. Certificate III

4. String public private key to public private key object support SM2 algorithm


BC package needed

    <!-- -->  
            <!-- -->  

Posted by justphpdev on Tue, 05 May 2020 09:42:11 -0700