Solved

base64 decoding

Posted on 2004-08-25
18
485 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:inzaghi
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11894849
Yes, I would have thought that this is enough.

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11894851
Yes. YOu must ask for thre correct encoding though
0
 

Author Comment

by:inzaghi
ID: 11897497
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
 

Author Comment

by:inzaghi
ID: 11897502
oops this refers to

>>Yes. YOu must ask for thre correct encoding though
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11897538
Can you remind me how you've *written* the atoms?
0
 

Author Comment

by:inzaghi
ID: 11897583
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11897717
>>( 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11897723
When i say 'the full solution' i mean the full code for handling that part of the reading back of course
0
 

Author Comment

by:inzaghi
ID: 11897816
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11897893
In what way does processString work (i don't know the class)?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11897930
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
 

Author Comment

by:inzaghi
ID: 11907663
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
 

Author Comment

by:inzaghi
ID: 11907794
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
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 11907970
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
 

Author Comment

by:inzaghi
ID: 11910815
Thanks objects, but why wont this work

String decodedS = new Base64Decoder(base64).processString();
byte[] b = decodedS.getBytes("UTF-8");
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 11910958
>>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
 
LVL 92

Expert Comment

by:objects
ID: 11918379
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11935221
8-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now