transmitting a publickey to a client...

I have created 2 keys, a public key and a private key (using DSA encoding algorithm)

along with a file signature

With these variables, I remotely "lock" a file, what I need to do is to be able to transmit the publicKey to the user.

What I did, was to write the signature and the PublicKey to a file, sent that file to my user (this is the best way for my app), and tried to allow the program to convert the text back into the signature and public key.

the signature is fine.

the publicKey always turns out wrong. I tried just reading it as text, but cannot do it that way, so I tried the following, always resulting in a null value...

BASE64Decoder decoder =new BASE64Decoder();

try{

byte[] decodedPublicKey = decoder.decodeBuffer(strSections[1]);

//bytes can be converted back to public key

X509EncodedKeySpec pubKeySpec =new X509EncodedKeySpec(decodedPublicKey);

KeyFactory keyFactory = KeyFactory.getInstance("DSA");

m_pkKey = keyFactory.generatePublic(pubKeySpec);

}catch(IOException ioe){

}catch(NoSuchAlgorithmException nsa){

}catch(InvalidKeySpecException ikse){

}

any ideas?

Message was edited by:

adamorn

[1734 byte] By [adamorna] at [2007-10-2 21:37:35]
# 1
> any ideas?> Swallowing exceptions like this is almost always wrong. Print a stack trace to find out which exception is being thrown and the cause of that exception.
sabre150a at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 2
I did mention that the public key was coming up wrong right? its coming up null to be more clear...
adamorna at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 3

> I did mention that the public key was coming up wrong

> right? its coming up null to be more clear...

I did mention that you need to print out the exceptions to find out what is happening right!

To be more clear, if you get an exception from any of the lines before the assignment to the public key then you will end up with a null public key! Right!

sabre150a at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 4
sorry..The key is coming up as null... after trying to convert it from text back to the publickey...
adamorna at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 5
> sorry..> > The key is coming up as null... after trying to> convert it from text back to the publickey...Which friggin line are you talking about?
sabre150a at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 6

after this code is run, the key is still null

the strSections[1] = the encoded key written as a String.

it is written as a string because it is stored in a file, and when I try to read the public key from the file, I must work with converting the text back into a publickey object...

the only thing is that even though the string value matches what the originial publicKey.getEncoded() value is, I cannot make it into a PublicKey Object :(

So I feel I must be going about this wrong...

Perhaps Im asking the wrong question? Perhaps I should be asking how to simply convert a PublicKey object into a string object, then back to a publicKey object?

Oh well, I again included the code with some more comments...

let me know if you think there is another way to do this...

thanks

BASE64Decoder decoder = new BASE64Decoder();

try{

//convert text to byte[] for publicKey

byte[] decodedPublicKey = decoder.decodeBuffer(strSections[1]);

//bytes can be converted back to public key

X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodedPublicKey);

//actually rebuild the public key

KeyFactory keyFactory = KeyFactory.getInstance("DSA");

m_pkKey = keyFactory.generatePublic(pubKeySpec);

}catch(IOException ioe){

}catch(NoSuchAlgorithmException nsa){

}catch(InvalidKeySpecException ikse){

}

adamorna at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 7

Until you stop swallowing the exceptions it is not worth me trying to help you. I can see several possibilities so my last words for the moment are

1) Do you fully read the Base64 encoded key from the file? Hint, it is no use just reading the first line since Base64 encoding typically splits the output every 72 characters.

2) I see no reason why you need to Base64 encode the key before writing it to the file.

3) Stop swallowing exception.

4) Print stack traces so you can see what is wrong.

5) Stop swallowing exception.

6) Print stack traces so you can see what is wrong.

7) Stop swallowing exception.

8) Print stack traces so you can see what is wrong.

9) Post the code for saving to a file.

10) Post the code for recovering from a file.

Bye

sabre150a at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 8

ok... I generate the keys.. then...

//1. append public key to to file, using a seperator via

File filLocked = new File("C:\\myFile.tutl");

String strLockedStrFile = signature + m_strLockedSeperator + publicKey.getEncoded() + m_strLockedSeperator + strFileContents;

ObjectOutputStream oos = null;

//Save

if (oos != null){

oos.writeObject(strLockedFile);

oos.flush();

oos.close();

//

//2. send the file, successful! now it is on the users computer in string form

try{

strSectionsOfFile = strFile.split(m_strSeperator);

//System.out.println(strSections[0]);

}catch (java.util.regex.PatternSyntaxException pse){

System.out.println(pse.getDescription());

System.out.println(pse.getMessage());

System.out.println(pse.getPattern());

System.out.println(pse.getIndex());

}

signature = strSectionsOfFile[0]; //success

//here is the problem - reconstructing the publicKey - it always turns out NULL!

BASE64Decoder decoder = new BASE64Decoder();

try{

byte[] decodedPublicKey = decoder.decodeBuffer(strSections[1]);

//bytes can be converted back to public key

X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodedPublicKey);

KeyFactory keyFactory = KeyFactory.getInstance("DSA");

m_pkKey = keyFactory.generatePublic(pubKeySpec);

}catch(IOException ioe){

}catch(NoSuchAlgorithmException nsa){

}catch(InvalidKeySpecException ikse){

}

again thanks for the help!!! i really appreciate it

adamorna at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 9

This line

String strLockedStrFile = signature + m_strLockedSeperator + publicKey.getEncoded() + m_strLockedSeperator + strFileContents;

does not in any ways represent the key. The term

publicKey.getEncoded()

is a byte array and adding this

m_strLockedSeperator + publicKey.getEncoded() + m_strLockedSeperator will just use the pseudo reference to the array and not the content of the array which is what you want.

I don't know how you have defined signature but if it is also a byte array then your signature storage will also be wrong.

The line

byte[] decodedPublicKey = decoder.decodeBuffer(strSections[1]);

assumes that publicKey.getEncoded() gives you a Base64 encode result. It does not so even if the split was in some way providing the publicKey.getEncoded() this still would not work.

The code

}catch(IOException ioe){

}catch(NoSuchAlgorithmException nsa){

}catch(InvalidKeySpecException ikse){

}

is still swallowing exceptions. I don't understand why you persist in this ridiculous practice.

Why don't you just write two simple objects to the output stream. The signature and byte array obtained from publicKey.getEncoded(). You can then just read the objects from the file and you won't have to go though all the contortions of splitting the content of the file.

Your code indicates that you are well out of your depth. I suggest hiring someone who is is not.

sabre150a at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 10

I got it!

all I had to do was write the public key to file as a string, then in the file reading portion, read the string into a string object, and in the base64 line, use the stringObject.getEncoded()!

it works, your information helped guide me to come up with the final solution. I appreciate your help but will not be hiring out my own work.

thanks

yes, I am new to sending cryptology over the web

adamorna at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...
# 11
oh and as far as out of my depth is concerned - I knew what the problem was, that is why I was not writing out the other exceptions. I knew that it was NULL when reading from the file, the excpetion would not have been caught becauseI was not catching the Null Exception
adamorna at 2007-7-14 0:51:54 > top of Java-index,Security,Cryptography...