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

Ash*_*hah 9 java cryptography rsa pkcs#1

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

mar*_*jno 14

使用Bouncy Castle SubjectPublicKeyInfo,就像这样:

byte[] encoded = publicKey.getEncoded();
SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(
    ASN1Sequence.getInstance(encoded));
byte[] otherEncoded = subjectPublicKeyInfo.parsePublicKey().getEncoded();
Run Code Online (Sandbox Code Playgroud)


小智 5

没有 BouncyCastle:

PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyBinary));                
Run Code Online (Sandbox Code Playgroud)