Solved

help with MD5'ing a string

Posted on 2006-06-09
13
298 Views
Last Modified: 2010-03-31
here is my code.  im not sure if im right...

package org;

import java.net.URL;
import java.net.URLEncoder;

import javax.swing.JApplet;

public class LDAPConnector extends JApplet {  
    public LDAPConnector() {}
    public void start() {
        try {
        String handshake = "xyz";
        String key = "123";
       
        MessageDigest md = MessageDigest.getInstance( "MD5" );
        String md5handshake = md.update( handshake );
        String md5key = md.update( key );

            this.getAppletContext().showDocument( new URL( "http://192.168.0.2:7810/index.php?" + URLEncoder.encode( "username", "UTF-8" ) + "=" + URLEncoder.encode( System.getProperty( "user.name" ) + "&" + URLEncoder.encode( "handshake", "UTF-8" ) + "=" + URLEncoder.encode( md5hankshake ) + "&" + URLEncoder.encode( "key", "UTF-8" ) + "=" + URLEncoder.encode( md5key ), "UTF-8" ) ), "_self" ) ;
        } catch ( Exception ex ) {
            ex.printStackTrace() ;
        }
    }
}
0
Comment
Question by:ellandrd
[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
13 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16871033
What is it that you are trying to achieve :) ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16871040
If those are the kinds of parameters which the URL expects in a GET request, then it looks fine....
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16871077
>> System.getProperty( "user.name" )

Not sure if the applet can read that without signing. I could be wrong, though. Only a test will tell.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 16

Author Comment

by:ellandrd
ID: 16871107
the signed applet etc is all taken care off!.

all i want to do is md5 the handshake and key values in the URL...
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 16871152
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16871190
how about:

package org;

import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;

import javax.swing.JApplet;

public class LDAPConnector extends JApplet
{  
      public LDAPConnector()
      {
            
      }
      
    public void start()
    {
        try
        {      
              byte[] handshake = "2grumble8".getBytes("UTF-8");
              byte[] key = "lqv78eke".getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance( "MD5" );
            byte[] md5handshake = md.update( handshake );
            byte[] md5key = md.update( key );
           
            String secretHandShake = "";
            String secretKey = "";
           
            for ( byte b : md5handshake )
            {
                  secretHandShake += Integer.toHexString( b & 0xff ) ;
            }
           
            for ( byte b : md5key )
            {
                  secretKey += Integer.toHexString( b & 0xff ) ;
            }
           
           
            this.getAppletContext().showDocument( new URL( "http://192.168.0.2:7810/index.php?" + URLEncoder.encode( "username", "UTF-8" ) + "=" + URLEncoder.encode( System.getProperty( "user.name" ) + "&" + URLEncoder.encode( "handshake", "UTF-8" ) + "=" + URLEncoder.encode( secretHandShake ) + "&" + URLEncoder.encode( "key", "UTF-8" ) + "=" + URLEncoder.encode( secretKey ), "UTF-8" ) ), "_self" ) ;
        }
        catch ( Exception ex )
        {
            ex.printStackTrace() ;
        }
    }
}
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16871201
using BASE64 encode is fine, but how would i check this value against another encryted value in PHP?

does PHP support BASE64?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16871212
Not sure, that needs to be asked in the PHP topic area :) or post a link to this Q on it.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16871215
(my assumption is that it should)
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16871294
yes i does i just found a php function that decodes it.

i now need to get my java code working...

these are my errors:

C:\Pegasus\Intranet Stuff\src\org\LDAPConnector.java:24: cannot find symbol
symbol  : class KeyGenerator
location: class org.LDAPConnector
            KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
            ^
C:\Pegasus\Intranet Stuff\src\org\LDAPConnector.java:24: cannot find symbol
symbol  : variable KeyGenerator
location: class org.LDAPConnector
            KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
                                  ^
C:\Pegasus\Intranet Stuff\src\org\LDAPConnector.java:25: cannot find symbol
symbol  : class SecretKey
location: class org.LDAPConnector
            SecretKey key = keyGen.generateKey();
            ^
C:\Pegasus\Intranet Stuff\src\org\LDAPConnector.java:28: cannot find symbol
symbol  : class Mac
location: class org.LDAPConnector
            Mac mac = Mac.getInstance(key.getAlgorithm());
            ^
C:\Pegasus\Intranet Stuff\src\org\LDAPConnector.java:28: cannot find symbol
symbol  : variable Mac
location: class org.LDAPConnector
            Mac mac = Mac.getInstance(key.getAlgorithm());
                      ^
Note: C:\Pegasus\Intranet Stuff\src\org\LDAPConnector.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
5 errors

Tool completed with exit code 1


code:


        // Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104
            // In practice, you would save this key.
            KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
            SecretKey key = keyGen.generateKey();
           
            // Create a MAC object using HMAC-MD5 and initialize with key
            Mac mac = Mac.getInstance(key.getAlgorithm());
            mac.init(key);
           
            String handshake = "2grumble8";
            String keys = "lqv78eke";
           
            // Encode the string into bytes using utf-8 and digest it
            byte[] utf81 = handshake.getBytes("UTF8");
            byte[] digest1 = mac.doFinal(utf81);
           
            byte[] utf82 = keys.getBytes("UTF8");
            byte[] digest2 = mac.doFinal(utf82);
           
            // If desired, convert the digest into a string
            String b64HandShake = new sun.misc.BASE64Encoder().encode(digest1);
            String b64Key = new sun.misc.BASE64Encoder().encode(digest2);
           
         
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16871575
Your java version must be prior to 1.4, cause all these classes were introduced in Java version 1.4, see the API docs, and try running this from a command line:

java -version
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16871787
did you import javax.crypto.* ?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16876544
>>did you import javax.crypto.*?

Yes

>>java -version

java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 96
Notify sent to other threads in Java 9 54
netbeans can not run self signed app 8 37
How to fix  socket closed error 11 64
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

734 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