Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

M

Status
Not open for further replies.

Kavius

Programmer
Apr 11, 2002
322
CA
After the last question I asked I am a little gun shy (stupid question)...but this time I have looked into it in the books and online and they all say I am doing it right. Unfortunately, it doesn't work, so I must not be doing it right... What am I missing?

This method is being called from a button click on an applet. The password box sends in the password they typed and then setPassword is supposed to encrypt the password before storing it:
Code:
public void setPassword(byte[] pass)
{
  try {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(pass);
    pass = md.digest();
    password = new sun.misc.BASE64Encoder().encode(pass);
  }
  catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
  }
  catch(Exception e){
    e.printStackTrace();
  }
}
When I run the applet, I am getting an "Access Denied" error (the debugger I am using is not that helpful). I tracked the error down to the declaration of the MessageDigest (single step...didn't crash...single step...didn't crash...single step...CRASH!!). I don't receive any error messages except that the debugger had an internal error retrieving the stack trace (isn't that wonderful?).

I did see one post about applets not playing well with BASE64ENCODER, but that's as close as I got.

Does anyone see what I am doing wrong?
 
error or exception? can you describe how the error is returned to you? I don't remember if the "SHA-1" algorithm is in the default Sun provider or whether it's in the one you have to download (unless you're using the jre that comes with jdk1.4, in which case the java cryptography extension comes as standard). I'll have to check my java cryptography book tomorrow.. in the meantime, goodluck;) Liam Morley
lmorley@gdc.wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
Sorry it took so long. I kind of rely on my email catching these notifications and for some reason I didn't get one...

My bad. It's an exception (just switched from big VB project). The other thing I did is I no longer use BASE64ENCODER. It never did what I wanted it to do, I don't know what I was thinking. I wanted a hex conversion. Since I couldn't find one that didn't come with a third-party package, I just built my own.

Now this line
Code:
password = new sun.misc.BASE64Encoder().encode(pass);
reads like this
Code:
password = Hex.encode(pass)

I don't know what the problem was. That is the only code that I can find that has changed and the whole thing now works. Well, not the whole thing. I have an SSL connection that keeps spitting in my face, but that's a completely different issue.

Thanks for your help.

In case anyone is looking at this in the future...
Code:
public void setPassword(byte[] pass)
{
  int i;

  try {
    //encrypt array
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(pass);
    pass = md.digest();
    //convert array to hex string
    password = MyProj.Hex.encode(pass);
    //release memory
    md = null;
  }
  catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
  }
  catch(Exception e){
    e.printStackTrace();
  }
        
  // clear out the byte array incase someone is looking
  for(i=0; i<pass.length; i++){
    pass[i] = 0;
  }
    
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top