retreiving a private key from a file

hi guys,

i have created a RSA key pair and have used the public key for encryption and converted the private key to base 64 and stored on it the system as a txt file.

publicstatic PublicKey keyGenerator()throws Exception{

//try{

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("rsa");

keyGen.initialize(512);

KeyPair KeyPair = keyGen.generateKeyPair();

PublicKey pubk = KeyPair.getPublic();

PrivateKey prvk = KeyPair.getPrivate();

saveprvKey(prvk);

//} catch(Exception ex) {

//System.out.println("Exception: " + ex);

//ex.printStackTrace();

//}

return pubk;

}

privatestaticvoid saveprvKey(PrivateKey prvky)throws Exception{

byte[] byteSeed = prvky.getEncoded();

BASE64Encoder encoder =new BASE64Encoder();

String seed = encoder.encodeBuffer(byteSeed);

FileWriter fw =new FileWriter("f:/privkey.txt");

fw.write(seed);

fw.close();

}

publicstatic PrivateKey loadPrvkey()throws Exception{

FileInputStream fis =new FileInputStream("f:/privkey.txt");

byte[] encKey =newbyte[fis.available()];

fis.read(encKey);

fis.close();

String seed =new String(encKey);

BASE64Decoder decoder =new BASE64Decoder();

byte[] byteSeed = decoder.decodeBuffer(seed);

PrivateKey prvKey =new PrivateKey(byteSeed);

return prvKey;

}

i am now having problems getting this back.

the line that it doesnt like is

PrivateKey prvKey =new PrivateKey(byteSeed);

once the key has been retrieved it is going to be sent to the decrypt method.

any help would be great!

[2878 byte] By [c4rtera] at [2007-11-27 1:07:45]
# 1
What is the "it"? And how can you tell that "it doesn't like" a line of code?Here's the point: if you're getting an error message, paste it in your post. Lame paraphrases are useless.
DrClapa at 2007-7-11 23:43:02 > top of Java-index,Java Essentials,Java Programming...
# 2

Sorry Dr

the error i get is shown below when i try and compile the class.

C:\Documents and Settings\cs31ac\JavaApplication1\src\Security.java:252: java.security.PrivateKey is abstract; cannot be instantiated

PrivateKey prvKey = new PrivateKey(byteSeed);

1 error

BUILD FAILED (total time: 0 seconds)

This is the method that i call the loadprvKey from.

public static byte[] decrypt(byte[] bytesIn) throws Exception{

System.out.println("in decrypt");

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

PrivateKey prvk = loadPrvkey();

System.out.println("read out" + prvk);

cipher.init(Cipher.DECRYPT_MODE, prvk);

c4rtera at 2007-7-11 23:43:02 > top of Java-index,Java Essentials,Java Programming...
# 3
cheers for the help i have managed to fix it now
c4rtera at 2007-7-11 23:43:02 > top of Java-index,Java Essentials,Java Programming...
# 4

One turns the bytes generated by the getEncoded() method into th private key usingPKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyAsBytes);

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

PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

BUT

your code to read the file is flawed. The use of available() to indicate the length of a file is poor at best. One should use File.length().

File pkfile = new File("f:/privkey.txt");

DataInputStream dis = new DataInputStream(new FileInputStream(pkfile));

byte[] encKey = new byte[(int)pkfile.length()];

dis.readFully(encKey);

dis.close();

Also, you would probably do better just writing the bytes of the key to file without base64 encoding them.

Edit: :-( I'm a bit slow today (several hours even).

Message was edited by:

sabre150

sabre150a at 2007-7-11 23:43:02 > top of Java-index,Java Essentials,Java Programming...