KeyPairGenerator kpg = KeyPairGenerator.getInstance(\"RSA\");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
Key publicKey = kp.getPublic();
Key privateKey = kp.ge
Asymmetric keys like those from RSA are usually stored in X509 format. Therefor you can use X509EncodedKeySpec
instead.
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
.