How to convert from String to PublicKey?

后端 未结 1 1514
面向向阳花
面向向阳花 2020-12-24 14:44

I\'ve used the following code to convert the public and private key to a string

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(\"RSA\");
keyPairG         


        
相关标签:
1条回答
  • 2020-12-24 15:11

    I don't think you can use the SecretKeySpec with RSA.

    This should do:

    byte[] publicBytes = Base64.decodeBase64(publicK);
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey pubKey = keyFactory.generatePublic(keySpec);
    

    And to decode the private use PKCS8EncodedKeySpec

    0 讨论(0)
提交回复
热议问题