Solved

Converting Binary data to ASCII

Posted on 2001-07-09
9
439 Views
Last Modified: 2010-05-18
Experts,

I have a small issue. I am using HTTP Basic Authentication to enforce security in the website. I am able to get the "Authorization" header information in the Servlet request, but the data contained under that header is not in ASCII format. It is binary, I believe. Thus, when I try to perform the following code.

               try{
               String userInfo=new String();
               userInfo = authorization.substring(6).trim();
               BASE64Decoder decoder = new BASE64Decoder();
               namePassword = new String(decoder.decodeBuffer(userInfo));
               htmlOutputStream.print(namePassword);
               }
               catch (Exception e)
               {
                    htmlOutputStream.print(e.toString());
               }

               try
               {
                    int index = namePassword.indexOf(":");
                    user_text = namePassword.substring(0, index);
                   password_text = namePassword.substring(index+1);
               }
               catch (Exception ce)
               {
                    htmlOutputStream.print(ce.toString());
               }

here, namePassword is returning data in binary format, so the ce exception is showing "String index out of bounds"??

Is there any way to convert namePassword to ASCII format, so that I can exteact the user name and password??

please let me know.
0
Comment
Question by:gvijay1
9 Comments
 

Author Comment

by:gvijay1
ID: 6265988
Another note, I am using Web Sphere as the webserver, so I am not sure if this makes a difference.
0
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6266308
Hi!

Try the following:
namePassword = String.valueOf(decoder.decodeBuffer(userInfo));

Print the value of 'namePassword' to check that it actually contain a ':'. In case it is not, the substring(0, -1) will fail with an exception.

In that case, incorporate the following changes:

try
  {
     int index = namePassword.indexOf(":");
     if (index >= 0)
     {
     user_text = namePassword.substring(0, index);
     password_text = namePassword.substring(index+1);
     }
  }
  catch (Exception ce)
              {
                   htmlOutputStream.print(ce.toString());
              }


Hope that helps!
             
0
 

Author Comment

by:gvijay1
ID: 6266572
Hi Expert,

I tried the code u had posted and this is what happened. When I used my initial line of code of;
namePassword = new String(decoder.decodeBuffer(userInfo)), namePassword returned with ?ea?????EA?EE??

when I used the line you had given me of;
namePassword = String.valueOf(decoder.decodeBuffer(userInfo));, name Password retuned with [B@391ea99

So, there is obviously a difference. It seems like it is decoding it into ASCII, but it still does not contain the userID and password that was entered in. currently, I have given the realm name some random value...does the realm name matter? Is this causing the problem??
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:sghosh092199
ID: 6267002
Hi!

Since you are decoding, I believe that the encoding has taken place somewhere already. Check that the encoder  of type BASE64Encoder has got the appropriate values for userID and password.

Secondly, it seems that namePassword is a concatenation of userID, a ':' and a password. In case they have been concatenated and then encoded, it will not hold the ':' character in encoded form.

I suppose you are using javax.commerce.util.BASE64Decoder.
byte     mydata[];
     BASE64Decoder base64 = new BASE64Decoder();
     mydata = base64.decodeBuffer(encryptedInfo);
So, what you are getting as output is an array of bytes.
You can do a new String(mydata);
 

             
0
 

Author Comment

by:gvijay1
ID: 6269843
Hi again,

Actually, I am using the HTTP Basic Authentication within the webserver, so the userID and password are automatically encoded and sent back to the servlet under the Authorization header. Please correct me if I am wrong.

I checked the Core Servlets book and there, it seems like the user ID and password are always returned in the format of UserID:Password, and from the code there, it seems like the : is always encoded as well. I am not sure what the issue is. Does it have anything to do with the "realm-name" or does the web server need to be configured differently?

please let me know your thoughts.

 
0
 

Author Comment

by:gvijay1
ID: 6270255
Hi Expert,

I tried your suggestion, and it seems to be giving me the same kind of error. Please let me know if you have any other ideas that I can work with.

-gvijay1
0
 
LVL 3

Accepted Solution

by:
sghosh092199 earned 100 total points
ID: 6270470
What I could get is, the 'authorization' variable should have a value like encoded_str1:encoded_str2

In this case, can you print the values of authorization and userinfo?

Next, try the indexOf() before you decode. One of the reason may be it is trying to decode the ':' too.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6853279
Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20092733.html
http://www.experts-exchange.com/questions/Q.20147918.html
http://www.experts-exchange.com/questions/Q.20168330.html
http://www.experts-exchange.com/questions/Q.20205426.html
http://www.experts-exchange.com/questions/Q.20265649.html
http://www.experts-exchange.com/questions/Q.20266870.html
http://www.experts-exchange.com/questions/Q.20269541.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20103690.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9017426
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[points to sghosh]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I remove an object from a 3 53
What's wrong with this code? 4 36
Java 8 to Java 6 8 33
dao vs facade design patterns 2 35
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

733 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