Solved

help with MD5'ing a string

Posted on 2006-06-09
13
294 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace a word with other 1 43
jboss wildfly 10.1 10 75
How to determine if a string is a valid SHA value 7 28
JAVA API design with micro service cloud in mind 1 10
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now