?
Solved

Converting Binary data to ASCII

Posted on 2001-07-09
9
Medium Priority
?
443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

764 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