Serialize and Deserialize an RSA public key

后端 未结 3 1250
耶瑟儿~
耶瑟儿~ 2021-02-08 13:48
KeyPairGenerator kpg = KeyPairGenerator.getInstance(\"RSA\");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
Key publicKey = kp.getPublic();
Key privateKey = kp.ge         


        
3条回答
  •  滥情空心
    2021-02-08 14:17

    Asymmetric keys like those from RSA are usually stored in X509 format. Therefor you can use X509EncodedKeySpecinstead.

    A simple example is already in the Java 7 JavaDoc (just replace DSA with RSA): http://docs.oracle.com/javase/7/docs/api/java/security/KeyFactory.html

    X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
    

    If you need to deserialize the private from byte[], I've found that you must use PKCS8EncodedKeySpec.

提交回复
热议问题