Publickey from bouncycastle RSAKeyParameters

2019-06-10 09:29发布

问题:

I have the following code that reads a CSR from a string and outputs the public key in a PEM format. I also need to get the public key as java RSAPublickey but I can't work out how to do this.

import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import org.bouncycastle.util.io.pem.PemWriter;

import java.io.*;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;


    public class parseCSRfromString {


        String csrPEM = "-----BEGIN CERTIFICATE REQUEST-----\n" +
                "MIICwjCCAaoCAQAwfTELMAkGA1UEBhMCVVMxEDAOBgNVBAgTB0Zsb3JpZGExEzAR\n" +
                "BgNVBAcTCkJvY2EgUmF0b24xGzAZBgNVBAoTEkxvb25leSBUb29ucywgSW5jLjEU\n" +
                "MBIGA1UECxMLRGV2ZWxvcG1lbnQxFDASBgNVBAMTC2V4YW1wbGUuY29tMIIBIjAN\n" +
                "BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiJCj31d1Rp+aKz/GTkedaiS/VSCc\n" +
                "PRARYgXukobjgBHx46HjldAcfg/DoANn5lEQaFxaIZJLbZ/AdLUyw/hUbU0CjWXv\n" +
                "pN3Ep3o9XgRTPkIFoI22VOI/O2ZLjBq/E4DWyVmv+vG6BK0LRh7hykzPCw6KIRR9\n" +
                "NCmUMJMQX5d5P/r1lR5H399pnLcLsrHoWDwBSEDgkGWyxnvEB0+/bIz42T3qnlFt\n" +
                "7avarxlHG2p5DoRTf8GJ+6imY88ZeBW/Nk18aDINsAHWLv383JICIAsZ3VuMk8m/\n" +
                "Z/Z5b21zIuZECDJjZjvAAjr/shVLB+Pck5+HJy6tqj79MJOQu+jKIrK8VwIDAQAB\n" +
                "oAAwDQYJKoZIhvcNAQEFBQADggEBAGtuAAHG4OC9jSRjGWSqfMXTDMz9tgekDREA\n" +
                "SYv5QIrOXsMzwbgDw8LxRJZEskl4JJOnjwEvUXWUF1M6XmG2h358nOnrkOlsumHw\n" +
                "Tx5gGSr6S6aJO/HG46erctE8aWpnFZYMfuEkul4ApsIufL7Bxqs3NHZWcrWBlLIP\n" +
                "aVCKx1FPRMC36Tj3EslbuUB/iTRt90Nfq1IxHMIKiwCiSNJSqfRVLANhI8MUbOjB\n" +
                "CBly1wcH68WWNkyvHVvbcF/B9AfYG9AqWjZjygKpyf81VZWctXhDc8UtomqrblXN\n" +
                "mvz4RKpIhZQLuuxlBrdzJkPm2sOdtdZghebCRRVWdjsig4sylgQ=\n" +
                "-----END CERTIFICATE REQUEST-----";


        StringReader csrstring = new StringReader(csrPEM);
        PemReader pemReader = new PemReader(csrstring);

        public String readCSR() {
            String theKey = new String();


            try {
                PKCS10CertificationRequest csr = new PKCS10CertificationRequest(pemReader.readPemObject().getContent());
                pemReader.close();


                // Write the Public Key as a PEM-File
                StringWriter output = new StringWriter();
                PemWriter pemWriter = new PemWriter(output);

                PemObject pkPemObject = new PemObject("PUBLIC KEY",
                        csr.getSubjectPublicKeyInfo().getEncoded());

                pemWriter.writeObject(pkPemObject);
                pemWriter.close();

                // System.out.println(output.getBuffer());
                theKey = output.getBuffer().toString();

                RSAKeyParameters pubkey = (RSAKeyParameters) PublicKeyFactory.createKey(csr.getSubjectPublicKeyInfo());

            } catch (IOException e) {
                e.printStackTrace();
            }
            return theKey;
        }


    }

回答1:

Build the public key using modulus and exponent from RSAKeyParameters

 RSAKeyParameters pubkey =...

 RSAPublicKeySpec spec = new RSAPublicKeySpec(pubkey.getModulus(), pubkey.getExponent());
 KeyFactory factory = KeyFactory.getInstance("RSA");
 PublicKey pub = factory.generatePublic(spec);