该PublicKey.getEncoded(),返回包含的SubjectPublicKeyInfo(X.509)格式的公钥的字节数组,我如何将其转换为RSA公共密钥编码?
Answer 1:
使用充气城堡的SubjectPublicKeyInfo
,就像这样:
byte[] encoded = publicKey.getEncoded();
SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(
ASN1Sequence.getInstance(encoded));
byte[] otherEncoded = subjectPublicKeyInfo.parsePublicKey().getEncoded();
Answer 2:
Without BouncyCastle:
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyBinary));
Answer 3:
下面的代码片段工作对我来说,不得不虽然使用BouncyCastle的。
byte[] keyBytes = key.getEncoded(); // X.509 for public key
SubjectPublicKeyInfo subPkInfo = new SubjectPublicKeyInfo((ASN1Sequence)ASN1Object.fromByteArray(keyBytes));
byte[] rsaformat = subPkInfo.getPublicKey().getDEREncoded();
文章来源: Converting A public key in SubjectPublicKeyInfo format to RSAPublicKey format java