Converting A public key in SubjectPublicKeyInfo format to RSAPublicKey format java

后端 未结 3 461
北恋
北恋 2020-12-14 23:17

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?

3条回答
  •  醉话见心
    2020-12-14 23:55

    The following snippet of code worked for me, had to use BouncyCastle though.

    byte[] keyBytes = key.getEncoded(); // X.509 for public key
    SubjectPublicKeyInfo subPkInfo = new SubjectPublicKeyInfo((ASN1Sequence)ASN1Object.fromByteArray(keyBytes));
    byte[] rsaformat = subPkInfo.getPublicKey().getDEREncoded();
    

提交回复
热议问题