我正在尝试从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但它只是调用最后一行。
答案 0 :(得分:4)
我会回答我自己的问题:
byte[] encoded=publicKey.getEncoded();
SubjectPublicKeyInfo subPkInfo2 = new SubjectPublicKeyInfo(rsaEncryption, encoded);
这是错误的。 PublicKey已经是ASN1typeSubjectpublicKeyInfo。为了使用此函数创建SubjectPublicKeyInfo
SubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] publicKey)
您需要编码形式的Algortihm标识符和KEY ITSELF。 publicKey.getEncoded()是一个编码的KEY +算法标识符。
反正。如果您想将SATSA公钥转换为主题公钥信息,请执行以下操作:
byte[] publickeyb=SATSApublickey.getEncoded();
SubjectPublicKeyInfo subPkInfo = new SubjectPublicKeyInfo((ASN1Sequence)ASN1Object.fromByteArray(publickeyb));
如果你想从PrincipPublicKeyInfo获取你的公钥作为RSAkeyparameters
RSAKeyParameters param=(RSAKeyParameters) PublicKeyFactory.createKey(subPkInfo);