将 SubjectPublicKeyInfo 格式的公钥转换为 RSAPublicKey 格式 java

2022-01-10 00:00:00 cryptography rsa java pkcs#1

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

The PublicKey.getEncoded(), returns a byte array containing the public key in SubjectPublicKeyInfo (x.509) format, how do i convert it to RSA public key encoding?

推荐答案

使用 Bouncy Castle 的 SubjectPublicKeyInfo,像这样:

Use Bouncy Castle's SubjectPublicKeyInfo, like this:

byte[] encoded = publicKey.getEncoded();
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(
        ASN1Sequence.getInstance(encoded));
byte[] otherEncoded = subjectPublicKeyInfo.parsePublicKey().getEncoded();

相关文章