tags:

views:

635

answers:

3

Currently when I make a signature using java.security.signature, it passes back a string. I can't seem to use this string since there are special characters that can only be seen when i copy the string into notepad++, from there if I remove these special characters I can use the remains of the string in my program.

In notepad they look like black boxes with the words ACK GS STX SI SUB ETB BS VT

I don't really understand what they are so its hard to tell how to get ride of them.

Is there a function that i can run to remove these and potentially similar characters?

when i use the base64 class supplied in the posts, i cant go back to a signature

System.out.println(signature);
String base64 = Base64.encodeBytes(sig);
System.out.println(base64);
String sig2 = new String (Base64.decode(base64));
System.out.println(sig2);

gives the output

”zÌý¥y]žd”xKmËY³ÕN´Ìå}ÏBÊNÈ›`Αrp~jÖüñ0…Rõ…•éh?ÞÀ_û_¥ÂçªsÂk{6H7œÉ/”âtTK±Ï…Ã/Ùê²
lHrM/aV5XZ5klHhLbctZs9VOtMzlfc9Cyk7Im2DOkXJwfmoG1vzxMIVS9YWV6Wg/HQLewF/7X6XC56pzwmt7DzZIN5zJL5TidFRLsc+Fwy/Z6rIaNA2uVlCh3XYkWcu882tKt2RySSkn1heWhG0IeNNfopAvbmHDlgszaWaXYzY=
[B@15356d5
+5  A: 

How are you "making" the signature? If you use the sign method, you get back a byte array, not a string. That's not a binary representation of some text, it's just arbitrary binary data. That's what you should use, and if you need to convert it into a string you should use a base64 conversion to avoid data corruption.

Jon Skeet
do i base64 convert the signatures byte array?
ChronoXIII
+4  A: 

The odd characters are there because cryptographic signatures produce bytes rather than strings. Consequently if you want a printable representation you should Base64 encode it (here's a public domain implementation for Java).

Stripping the non-printing characters from a cryptographic signature will render it useless as you will be unable to use it for verification.

Update:

[B@15356d5

This is the result of toString called on a byte array. "[" means array, "B" means byte and "15356d5" is the address of the array. You should be passing the array you get out of decode to Signature.verify.

Something like:

Signature sig = new Signature("dsa");
sig.initVerify(key);
sig.verify(Base64.decode(base64)); // <-- bytes go here
Aaron Maenpaa
does the base64 conversion of a signature still hold up as a valid signature from the technical stand point?
ChronoXIII
It will contain all the data, that's what counts. The base64 encoding will have to be reversed before using it to check the signature's validity.
Michael Borgwardt
I'm having issues changing the base64 conversion to a signature again, I've updated the my intial post
ChronoXIII
A: 

If I understand your problem correctly, you need to get rid of characters with code below 32, except maybe char 9 (tab), char 10 (new line) and char 13 (return).

Edit: I agree with the others as handling a crypto output like this is not what you usually want.

kd304
"Getting rid of" data in a cryptographic signature is rarely a good idea... I strongly suspect it's a more fundamental problem.
Jon Skeet
I was wondering too.
kd304
what do you mean by code below 32
ChronoXIII
kd304