本文介绍了bouncycastle RSAKeyParameters的公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,该代码从字符串中读取CSR并以PEM格式输出公钥。我还需要以java RSAPublickey
的形式获取公钥,但是我无法解决该问题。
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;
}
}
推荐答案
使用RSAKeyParameters中的模数和指数构建公钥
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);
这篇关于bouncycastle RSAKeyParameters的公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!