Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Converting Binary data to ASCII

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
gvijay1
Asked:
gvijay1
1 Solution
 
gvijay1Author Commented:
Another note, I am using Web Sphere as the webserver, so I am not sure if this makes a difference.
0
 
sghosh092199Commented:
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
 
gvijay1Author Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sghosh092199Commented:
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
 
gvijay1Author Commented:
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
 
gvijay1Author Commented:
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
 
sghosh092199Commented:
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
 
MoondancerCommented:
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
 
sudhakar_koundinyaCommented:
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
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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now