http://docs.oracle.com/javase/tutorial/security/apisign/vstep2.html

保存公钥:

X509EncodedKeySpec x509ks = new X509EncodedKeySpec(

publicKey.getEncoded());

FileOutputStream fos = new FileOutputStream(strPathFilePubKey);

fos.write(x509ks.getEncoded());

加载公钥:

byte[] encodedKey = IoUtils.toByteArray(new FileInputStream(strPathFilePubKey));

KeyFactory keyFactory = KeyFactory.getInstance("RSA",p);

X509EncodedKeySpec pkSpec = new X509EncodedKeySpec(

encodedKey);

PublicKey publicKey = keyFactory.generatePublic(pkSpec);

保存私钥:

PKCS8EncodedKeySpec pkcsKeySpec = new PKCS8EncodedKeySpec(

privateKey.getEncoded());

FileOutputStream fos = new FileOutputStream(strPathFilePrivbKey);

fos.write(pkcsKeySpec.getEncoded());

加载私钥:

byte[] encodedKey = IoUtils.toByteArray(new FileInputStream(strPathFilePrivKey));

KeyFactory keyFactory = KeyFactory.getInstance("RSA",p);

PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(

encodedKey);

PrivateKey privateKey = keyFactory.generatePrivate(privKeySpec);