Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help with MD5'ing a string

Posted on 2006-06-09
13
Medium Priority
?
302 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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