Solved

help with MD5'ing a string

Posted on 2006-06-09
13
297 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 46
How to configure empty element in XML Document parser? 15 44
Java program running SQL query 5 48
batch vs regular insert in spring DAO 2 22
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 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