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.

[917 byte] By [deepali8] at [2007-9-27 0:14:02]
# 1

InvalidKeySpecException - if the requested key specification is inappropriate for the given key, or the given key cannot be processed (e.g., the given key has an unrecognized algorithm or format).

It is important to mention what key (object sk) you are passing to getKeySpec. What is sk and is PKCS8EncodedKeySpec appropriate for that key?

regds,

CA

caffeineAddict at 2007-7-4 15:15:18 > top of Java-index,Core,Core APIs...