从SubjectPublicKeyInfo获取公钥

发布于 2024-10-14 22:07:30 字数 711 浏览 8 评论 0原文

我正在尝试从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 但它只是调用最后一行。

I'm trying to get the public key from a SubjectPublicKeyInfo.However when I try:

PublicKey publicKey = k.generatePublic(keypsec);

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

It throws this

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)

Any ideas? I've tried PubliKeyFactory but it simply calls the last line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-10-21 22:07:30

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

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

这是错误的。 PublicKey 已经是 ASN1typeSubjectpublicKeyInfo。为了使用此函数

SubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] publicKey)

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

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

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

如果您想从主题公共密钥信息中获取作为 RSAkeyparameters 的公钥

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

Well I shall answer my own question:

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

This is wrong. PublicKey is already an ASN1typeSubjectpublicKeyInfo. In order to create a SubjectPublicKeyInfo with this function

SubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] publicKey)

you need an Algortihm identifier and THE KEY ITSELF in encoded form. publicKey.getEncoded() is an encoded KEY + THE ALGORITHM IDENTIFIER.

Anyway. If you want to tranfosrm a SATSA public key to a Subject Public Key Info you do this:

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

And if you want to get your public key as RSAkeyparameters from the SubjectPublicKeyInfo

RSAKeyParameters param=(RSAKeyParameters) PublicKeyFactory.createKey(subPkInfo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文