base64 decoding

I am using the import w3c.tools.codec.Base64Decoder;

to decode a base64 string, the method
new Base64Decoder(base64string).processString() returns a string object I need a byte representation of this data.
Is it correct to assume that we jsut need to call getBytes() at the end?
inzaghiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
you need something more like:

InputStream in = new ByteArrayInputStream(base64.getBytes("UTF-8"));
OutputStream out = new ByteArrayOutputStream();
new Base64Decoder(in, out).process();
in.close();
out.close();
byte[] encrypted = out.toByteArray();


told you life would be simpler if you avoided encoding altogether ;)
0
 
girionisCommented:
Yes, I would have thought that this is enough.

Regards
0
 
CEHJCommented:
Yes. YOu must ask for thre correct encoding though
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
inzaghiAuthor Commented:
Will I still need to do this even if when I open the file for reading the data from as

br = new BufferedReader( new InputStreamReader( f, "UTF-8)) );
String str = br.readLine();
String decode = new Base64Decoder(str.processString();
byte[] b = decode.getBytes();
0
 
inzaghiAuthor Commented:
oops this refers to

>>Yes. YOu must ask for thre correct encoding though
0
 
CEHJCommented:
Can you remind me how you've *written* the atoms?
0
 
inzaghiAuthor Commented:
username + \u241e + encrypteddata

I have tokenized the string based on the u241e
and to get the encypteddata back into byte data I have done the above ( i haven't shown  the full solution above though)
0
 
CEHJCommented:
>>( i haven't shown  the full solution above though)

Well that's what's confusing me! Can you do so (and repeat what your quesition is ;-))?
0
 
CEHJCommented:
When i say 'the full solution' i mean the full code for handling that part of the reading back of course
0
 
inzaghiAuthor Commented:
br = new BufferedReader( new InputStreamReader( f, "UTF-8)) );
while (str = br.readLine!=null){
 StringTokenizer st = new StringTokenizer(str, " \u241e");
 String user= st.nextToken();
 String base64 = st.nextToken();
String decodedS = new Base64Decoder(base64).processString();
byte[] b = decodedS.getBytes("UTF-8");
}                  }
0
 
CEHJCommented:
In what way does processString work (i don't know the class)?
0
 
CEHJCommented:
Forget that - i checked it. No - that call won't do as you originally had an encrypted byte array there, not a String, as far as i can remember
0
 
inzaghiAuthor Commented:
sorry 4 the late reply,

I am trying to get the original encrypted byte array.

Originall the byte array was encoded to base64.
I know need to decode it and get the bytes.

Dont understand why the following will not work?
String decodedS = new Base64Decoder(base64).processString();
byte[] b = decodedS.getBytes("UTF-8");

0
 
inzaghiAuthor Commented:
The actual string consists
of the username + seperator + encryptedData

The encrypted data is a byte array which has been encoded using base64

I don't understand why
String decodedS = new Base64Decoder(base64).processString();
byte[] b = decodedS.getBytes("UTF-8");


will not work?
0
 
inzaghiAuthor Commented:
Thanks objects, but why wont this work

String decodedS = new Base64Decoder(base64).processString();
byte[] b = decodedS.getBytes("UTF-8");
0
 
CEHJConnect With a Mentor Commented:
>>Thanks objects, but why wont this work

It's just that i'm a bit suspicious of how an array of 'binary' data is going to be handled as a String. It *may* be OK (and we can test it [more later]), but this:

>>byte[] b = decodedS.getBytes("UTF-8");

strikes me as wrong. Isn't what's being decoded at this point the encrypted data? If so, why is it being decrypted as UTF-8? It certainly won't *en*crypt properly in UTF-8. The array should have just been base64'd - nothing else
0
 
objectsCommented:
> Thanks objects, but why wont this work

Cause there is additional encoding involved in converting between byte arrays and Strings which will corrupt your data.
0
 
CEHJCommented:
8-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.