从SubjectPublicKeyInfo获取公钥

问题描述 投票:0回答:2

我正在尝试从SubjectPublicKeyInfo 获取公钥。但是当我尝试时:

PublicKey publicKey = k.generatePublic(keypsec);

byte[] encoded=publicKey.getEncoded();
SubjectPublicKeyInfo subPkInfo2 = new SubjectPublicKeyInfo(rsaEncryption, encoded);     
RSAPublicKeyStructure  pubKey = new RSAPublicKeyStructure((ASN1Sequence)subPkInfo2.getPublicKey());

它抛出这个

java.lang.IllegalArgumentException: illegal object in getInstance: org.bouncycastle.asn1.DERSequence
    at org.bouncycastle.asn1.DERInteger.getInstance(DERInteger.java:37)
    at org.bouncycastle.asn1.x509.RSAPublicKeyStructure.<init>(RSAPublicKeyStructure.java:63)

有什么想法吗?我尝试过 PubliKeyFactory 但它只是调用最后一行。

java-me bouncycastle public-key
2个回答
5
投票

好吧,我会回答我自己的问题:

byte[] encoded=publicKey.getEncoded();
SubjectPublicKeyInfo subPkInfo2 = new SubjectPublicKeyInfo(rsaEncryption, encoded); 

这是错误的。 PublicKey 已经是 ASN1typeSubjectpublicKeyInfo。为了用这个函数创建一个SubjectPublicKeyInfo

SubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] publicKey)

您需要一个算法标识符和编码形式的密钥本身。 publicKey.getEncoded() 是一个编码的密钥 + 算法标识符。

无论如何。如果您想将 SATSA 公钥转换为主题公钥信息,请执行以下操作:

byte[] publickeyb=SATSApublickey.getEncoded();
    SubjectPublicKeyInfo subPkInfo = new SubjectPublicKeyInfo((ASN1Sequence)ASN1Object.fromByteArray(publickeyb));

如果您想从SubjectPublicKeyInfo 获取作为 RSAkey 参数的公钥

RSAKeyParameters param=(RSAKeyParameters) PublicKeyFactory.createKey(subPkInfo);

0
投票

我有这个代码:

SubjectPublicKeyInfo subjectPublicKeyInfo = subjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()); X509v3CertificateBuilder certBuilder = 新 X509v3CertificateBuilder(新 X500Name(subjectDN), BigInteger.valueOf(System.currentTimeMillis()), startDate, endDate, new X500Name(subjectDN), subjectPublicKeyInfo);

    JcaContentSignerBuilder contentSignerBuilder = new JcaContentSignerBuilder("SHA256withRSA");
    ContentSigner contentSigner = contentSignerBuilder.build(keyPair.getPrivate());

在 Windows 上完美运行。然而在Linux上我有同样的错误,你在工厂中有未知的对象

有人可以帮忙吗?谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.