Solved

help with MD5'ing a string

Posted on 2006-06-09
13
292 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:mayankeagle
ID: 16871033
What is it that you are trying to achieve :) ?
0
 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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
 
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:
mayankeagle 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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:mayankeagle
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:mayankeagle
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

24 Experts available now in Live!

Get 1:1 Help Now