11

PublicKey.getEncoded(),返回一个字节数组,其中包含 SubjectPublicKeyInfo (x.509) 格式的公钥,如何将其转换为 RSA 公钥编码?

4

3 回答 3

17

使用 Bouncy Castle 的SubjectPublicKeyInfo,像这样:

byte[] encoded = publicKey.getEncoded();
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(
        ASN1Sequence.getInstance(encoded));
byte[] otherEncoded = subjectPublicKeyInfo.parsePublicKey().getEncoded();
于 2012-12-27T09:53:10.293 回答
5

没有 BouncyCastle:

PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyBinary));                
于 2013-02-28T14:05:16.220 回答
0

以下代码片段对我有用,但必须使用 BouncyCastle。

byte[] keyBytes = key.getEncoded(); // X.509 for public key
SubjectPublicKeyInfo subPkInfo = new SubjectPublicKeyInfo((ASN1Sequence)ASN1Object.fromByteArray(keyBytes));
byte[] rsaformat = subPkInfo.getPublicKey().getDEREncoded();
于 2012-12-28T10:23:26.453 回答