problem in saving and reading private key to/from file
Hi All,
I am trying to write RSA private key to the file .
Part of my code is
KeyFactory kf = KeyFactory.getInstance("RSA");
byte[] pkcs8EncodedKey;
pkcs8EncodedKey = ((PKCS8EncodedKeySpec)kf.getKeySpec (sk,PKCS8EncodedKeySpec.class)).getEncoded();
// writing the private key to output file
System.out.println("writing the private key to output file " + args[0]);
FileOutputStream fos = new FileOutputStream(args[0]);
byte[] b2 = new byte[1024];
int i2 = pkcs8EncodedKey.length;
System.out.println(" private key byte size " + i2);
while (i2 != -1)
{
fos.write(b2, 0, i2);
}
But after running this code this throws an exception as
Caught exception: java.security.spec.InvalidKeySpecException: Key spec does not match the key.
Please Guide.
Thanks.
Regards,
Deepali.

