C# MD5 Hash Does Not Match Hash Generated From Java

R

Ratfish

I've got a problem where a MD5 Hash function in .NET generates a
different hash than a similar routine in Java/Android. The Android
hash is calculated as "J0t��j#߸�bq-�",while the .NET hash is
calculated as "SjB0j+xqI9+4hxticS0Cjw==". Is this due to a character
set issue? Unicode versus non-unicode?

The code is below. Any suggestions would be greatly appreciated.

Aaron

=====================================
Here's the .NET code:

public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = encoding.GetBytes(sText);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
string sEncodedText = Convert.ToBase64String(encodedTextBytes);
return sEncodedText;
}

=====================================
Here's the Android code:

public class HMACMD5 {

private final String HMAC_MD5_NAME = "HmacMD5";

private SecretKeySpec sk;
private Mac mac;

public HMACMD5(byte[] key) throws GeneralSecurityException {
init(key);
}

public HMACMD5(String key) throws GeneralSecurityException {
init(EncodingUtils.getAsciiBytes(key));
}

private void init(byte[] key) throws GeneralSecurityException {
sk = new SecretKeySpec(key, HMAC_MD5_NAME);
mac = Mac.getInstance(HMAC_MD5_NAME);
mac.init(sk);
}

public byte[] ComputeHash(byte[] data) {
return mac.doFinal(data);
}

public byte[] ComputeHash(String data) {
return ComputeHash(EncodingUtils.getAsciiBytes(data));
}
}

public String encodeText(String sKey, String sSrc) throws Exception {
HMACMD5 hmacMD5 = new HMACMD5(sKey);
byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
return sEncodedText;
}
 
A

Arne Vajhøj

I've got a problem where a MD5 Hash function in .NET generates a
different hash than a similar routine in Java/Android. The Android
hash is calculated as "J0t��j#߸�bq-�", while the .NET hash is
calculated as "SjB0j+xqI9+4hxticS0Cjw==". Is this due to a character
set issue? Unicode versus non-unicode?

The code is below. Any suggestions would be greatly appreciated.
=====================================
Here's the .NET code:

public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = encoding.GetBytes(sText);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
string sEncodedText = Convert.ToBase64String(encodedTextBytes);
return sEncodedText;
}

=====================================
Here's the Android code:

public class HMACMD5 {

private final String HMAC_MD5_NAME = "HmacMD5";

private SecretKeySpec sk;
private Mac mac;

public HMACMD5(byte[] key) throws GeneralSecurityException {
init(key);
}

public HMACMD5(String key) throws GeneralSecurityException {
init(EncodingUtils.getAsciiBytes(key));
}

private void init(byte[] key) throws GeneralSecurityException {
sk = new SecretKeySpec(key, HMAC_MD5_NAME);
mac = Mac.getInstance(HMAC_MD5_NAME);
mac.init(sk);
}

public byte[] ComputeHash(byte[] data) {
return mac.doFinal(data);
}

public byte[] ComputeHash(String data) {
return ComputeHash(EncodingUtils.getAsciiBytes(data));
}
}

public String encodeText(String sKey, String sSrc) throws Exception {
HMACMD5 hmacMD5 = new HMACMD5(sKey);
byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
return sEncodedText;
}

Difficult to say based on just some of the code.

But I can see two potential sources for errors:
1) you use byte[] as key for .NET and String being converted to
byte[] as US-ASCII for Java
2) the .NET code gets encoding of data as input while the Java code
treat data as US-ASCII

The code below are:
- complete
- generates same result "QkY+XkbFsIqyaQp5jPuLjw==" on .NET 4.0 and Java
SE 1.6

I have modified the Java code slightly to:
- fix the above
- avoid using the StringUtil class [it should be easy to convert the
code back to use StringUtil]

Arne

====

using System;
using System.Text;
using System.Security.Cryptography;

namespace E
{
public class Program
{
public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = encoding.GetBytes(sText);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
string sEncodedText = Convert.ToBase64String(encodedTextBytes);
return sEncodedText;
}
public static void Main(string[] args)
{
Console.WriteLine(EncodeText(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
"This is a test!", Encoding.UTF8));
Console.ReadKey();
}
}
}

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

public class HMACFun {
public static String encodeText(byte[] key, String sSrc, String
encoding) throws Exception {
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = sSrc.getBytes(encoding);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
String sEncodedText = B64.b64encode(encodedTextBytes);
return sEncodedText;
}
public static void main(String[] args) throws Exception {
System.out.println(encodeText(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
"This is a test!","UTF-8"));
}
}

class HMACMD5 {
private final String HMAC_MD5_NAME = "HmacMD5";
private SecretKeySpec sk;
private Mac mac;
public HMACMD5(byte[] key) throws GeneralSecurityException {
init(key);
}
public HMACMD5(String key, String encoding) throws
GeneralSecurityException, UnsupportedEncodingException {
init(key.getBytes(encoding));
}
private void init(byte[] key) throws GeneralSecurityException {
sk = new SecretKeySpec(key, HMAC_MD5_NAME);
mac = Mac.getInstance(HMAC_MD5_NAME);
mac.init(sk);
}
public byte[] ComputeHash(byte[] data) {
return mac.doFinal(data);
}
public byte[] ComputeHash(String data, String encoding) throws
UnsupportedEncodingException {
return ComputeHash(data.getBytes(encoding));
}
}

class B64 {
public static String b64encode(byte[] b) throws MessagingException,
IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write(b);
b64os.close();
return new String(baos.toByteArray());
}
public static byte[] b64decode(String s) throws
MessagingException, IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
InputStream b64is = MimeUtility.decode(bais, "Base64");
byte[] tmp = new byte[s.length()];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return res;
}
}
 
R

Ratfish

I've got a problem where a MD5 Hash function in .NET generates a
different hash than a similar routine in Java/Android.  The Android
hash is calculated as "J0t j#߸ bq- ", while the .NET hash is
calculated as "SjB0j+xqI9+4hxticS0Cjw==".  Is this due to a character
set issue?  Unicode versus non-unicode?
The code is below.  Any suggestions would be greatly appreciated.
=====================================
Here's the .NET code:
public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
    HMACMD5 hmacMD5 = new HMACMD5(key);
    byte[] textBytes = encoding.GetBytes(sText);
    byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
    string sEncodedText = Convert.ToBase64String(encodedTextBytes);
    return sEncodedText;
}
=====================================
Here's the Android code:
public class HMACMD5 {
    private final String HMAC_MD5_NAME = "HmacMD5";
    private SecretKeySpec sk;
    private Mac mac;
    public HMACMD5(byte[] key) throws GeneralSecurityException {
       init(key);
    }
    public HMACMD5(String key) throws GeneralSecurityException {
       init(EncodingUtils.getAsciiBytes(key));
    }
    private void init(byte[] key) throws GeneralSecurityException {
       sk = new SecretKeySpec(key, HMAC_MD5_NAME);
       mac = Mac.getInstance(HMAC_MD5_NAME);
       mac.init(sk);
    }
    public byte[] ComputeHash(byte[] data) {
       return mac.doFinal(data);
    }
    public byte[] ComputeHash(String data) {
       return ComputeHash(EncodingUtils.getAsciiBytes(data));
    }
}
public String encodeText(String sKey, String sSrc) throws Exception {
    HMACMD5 hmacMD5 = new HMACMD5(sKey);
    byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
    byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
    String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
    return sEncodedText;
}

Difficult to say based on just some of the code.

But I can see two potential sources for errors:
1) you use byte[] as key for .NET and String being converted to
    byte[] as US-ASCII for Java
2) the .NET code gets encoding of data as input while the Java code
    treat data as US-ASCII

The code below are:
- complete
- generates same result "QkY+XkbFsIqyaQp5jPuLjw==" on .NET 4.0 and Java
SE 1.6

I have modified the Java code slightly to:
- fix the above
- avoid using the StringUtil class [it should be easy to convert the
code back to use StringUtil]

Arne

====

using System;
using System.Text;
using System.Security.Cryptography;

namespace E
{
        public class Program
        {
                public static string EncodeText(byte[] key, string sText, Encoding
encoding)
                {
                   HMACMD5 hmacMD5 = new HMACMD5(key);
                   byte[] textBytes = encoding.GetBytes(sText);
                   byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
                   string sEncodedText = Convert.ToBase64String(encodedTextBytes);
                   return sEncodedText;
                }
                public static void Main(string[] args)
                {
                        Console.WriteLine(EncodeText(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8},
"This is a test!", Encoding.UTF8));
                        Console.ReadKey();
                }
        }

}

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

public class HMACFun {
        public static String encodeText(byte[] key, String sSrc, String
encoding) throws Exception {
           HMACMD5 hmacMD5 = new HMACMD5(key);
           byte[] textBytes = sSrc.getBytes(encoding);
           byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
           String sEncodedText = B64.b64encode(encodedTextBytes);
           return sEncodedText;
        }
        public static void main(String[] args) throwsException {
                System.out.println(encodeText(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
"This is a test!","UTF-8"));
        }

}

class HMACMD5 {
        private final String HMAC_MD5_NAME = "HmacMD5";
        private SecretKeySpec sk;
        private Mac mac;
        public HMACMD5(byte[] key) throws GeneralSecurityException {
                init(key);
        }
        public HMACMD5(String key, String encoding) throws
GeneralSecurityException, UnsupportedEncodingException {
                init(key.getBytes(encoding));
        }
        private void init(byte[] key) throws GeneralSecurityException {
                sk = new SecretKeySpec(key, HMAC_MD5_NAME);
                mac = Mac.getInstance(HMAC_MD5_NAME);
                mac.init(sk);
        }
        public byte[] ComputeHash(byte[] data) {
                return mac.doFinal(data);
        }
        public byte[] ComputeHash(String data, Stringencoding) throws
UnsupportedEncodingException {
                return ComputeHash(data.getBytes(encoding));
        }

}

class B64 {
     public static String b64encode(byte[] b) throws MessagingException,
IOException  {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         OutputStream b64os = MimeUtility.encode(baos, "base64");
         b64os.write(b);
         b64os.close();
         return new String(baos.toByteArray());
      }
      public static byte[] b64decode(String s) throws
MessagingException, IOException  {
         ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
         InputStream b64is = MimeUtility.decode(bais, "Base64");
         byte[] tmp = new byte[s.length()];
         int n = b64is.read(tmp);
         byte[] res = new byte[n];
         System.arraycopy(tmp, 0, res, 0, n);
         return res;
      }

}


Thanks Arne for the detailed response. And the others who set me
straight. I understand the suggested changes, but am still getting
strange results on the Android side: I've included the full example
projects I'm using for .NET and Android, using Arne's test case, which
should produce "QkY+XkbFsIqyaQp5jPuLjw==" on the Java side, but
doesn't. I did not implement Arne's B64 class yet, but that does not
appear to be the problem. The bytes returned from the computeHash
call do not match the bytes returned from the equivalent .NET call.
Anyone know what's going on with the Android project. Why doesn't it
yield the correct result?

Aaron

Android Project
================
package org.example.hmacmd5;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView tv = new TextView(this);
try {
tv.setText(encodeText(new byte[] { 1, 2, 3, 4, 5, 6, 7,
8 }, "This is a test!", "UTF-8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(tv);
}

public String encodeText(byte[] key, String sSrc, String sEncoding)
throws Exception {
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = EncodingUtils.getBytes(sSrc, sEncoding);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
return sEncodedText;
}
}

package org.example.hmacmd5;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HMACMD5 {
private final String HMAC_MD5_NAME = "HmacMD5";
private SecretKeySpec sk;
private Mac mac;

public HMACMD5(byte[] key) throws GeneralSecurityException {
init(key);
}

public HMACMD5(String key, String encoding) throws
GeneralSecurityException, UnsupportedEncodingException {
init(key.getBytes(encoding));
}

private void init(byte[] key) throws GeneralSecurityException {
sk = new SecretKeySpec(key, HMAC_MD5_NAME);
mac = Mac.getInstance(HMAC_MD5_NAME);
mac.init(sk);
}

public byte[] ComputeHash(byte[] data) {
return mac.doFinal(data);
}

public byte[] ComputeHash(String data, String encoding) throws
UnsupportedEncodingException {
return ComputeHash(data.getBytes(encoding));
}
}

..NET code
===================
using System;
using System.Text;
using System.Security.Cryptography;

namespace HmacMD5Test
{
class Program
{
public static string EncodeText(byte[] key, string sText,
Encoding encoding)
{
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = encoding.GetBytes(sText);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
string sEncodedText =
Convert.ToBase64String(encodedTextBytes);
return sEncodedText;
}

static void Main(string[] args)
{
Console.WriteLine(EncodeText(new byte[] { 1, 2, 3, 4, 5, 6,
7, 8 }, "This is a test!", Encoding.UTF8));
Console.ReadKey();
}
}
}
 
R

Ratfish

On 6/5/2011 1:34 AM, Ratfish wrote:
I've got a problem where a MD5 Hash function in .NET generates a
different hash than a similar routine in Java/Android.  The Android
hash is calculated as "J0t j#߸ bq- ", while the .NET hash is
calculated as "SjB0j+xqI9+4hxticS0Cjw==".  Is this due to a character
set issue?  Unicode versus non-unicode?
The code is below.  Any suggestions would be greatly appreciated..
=====================================
Here's the .NET code:
public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
    HMACMD5 hmacMD5 = new HMACMD5(key);
    byte[] textBytes = encoding.GetBytes(sText);
    byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
    string sEncodedText = Convert.ToBase64String(encodedTextBytes);
    return sEncodedText;
}
=====================================
Here's the Android code:
public class HMACMD5 {
    private final String HMAC_MD5_NAME = "HmacMD5";
    private SecretKeySpec sk;
    private Mac mac;
    public HMACMD5(byte[] key) throws GeneralSecurityException {
       init(key);
    }
    public HMACMD5(String key) throws GeneralSecurityException {
       init(EncodingUtils.getAsciiBytes(key));
    }
    private void init(byte[] key) throws GeneralSecurityException {
       sk = new SecretKeySpec(key, HMAC_MD5_NAME);
       mac = Mac.getInstance(HMAC_MD5_NAME);
       mac.init(sk);
    }
    public byte[] ComputeHash(byte[] data) {
       return mac.doFinal(data);
    }
    public byte[] ComputeHash(String data) {
       return ComputeHash(EncodingUtils.getAsciiBytes(data));
    }
}
public String encodeText(String sKey, String sSrc) throws Exception {
    HMACMD5 hmacMD5 = new HMACMD5(sKey);
    byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
    byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
    String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
    return sEncodedText;
}
Difficult to say based on just some of the code.
But I can see two potential sources for errors:
1) you use byte[] as key for .NET and String being converted to
    byte[] as US-ASCII for Java
2) the .NET code gets encoding of data as input while the Java code
    treat data as US-ASCII
The code below are:
- complete
- generates same result "QkY+XkbFsIqyaQp5jPuLjw==" on .NET 4.0 and Java
SE 1.6
I have modified the Java code slightly to:
- fix the above
- avoid using the StringUtil class [it should be easy to convert the
code back to use StringUtil]

using System;
using System.Text;
using System.Security.Cryptography;
namespace E
{
        public class Program
        {
                public static string EncodeText(byte[] key, string sText, Encoding
encoding)
                {
                   HMACMD5 hmacMD5 = new HMACMD5(key);
                   byte[] textBytes = encoding.GetBytes(sText);
                   byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
                   string sEncodedText = Convert.ToBase64String(encodedTextBytes);
                   return sEncodedText;
                }
                public static void Main(string[] args)
                {
                        Console.WriteLine(EncodeText(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
"This is a test!", Encoding.UTF8));
                        Console.ReadKey();
                }
        }

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;
public class HMACFun {
        public static String encodeText(byte[] key,String sSrc, String
encoding) throws Exception {
           HMACMD5 hmacMD5 = new HMACMD5(key);
           byte[] textBytes = sSrc.getBytes(encoding);
           byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
           String sEncodedText = B64.b64encode(encodedTextBytes);
           return sEncodedText;
        }
        public static void main(String[] args) throws Exception {
                System.out.println(encodeText(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
"This is a test!","UTF-8"));
        }

class HMACMD5 {
        private final String HMAC_MD5_NAME = "HmacMD5";
        private SecretKeySpec sk;
        private Mac mac;
        public HMACMD5(byte[] key) throws GeneralSecurityException {
                init(key);
        }
        public HMACMD5(String key, String encoding)throws
GeneralSecurityException, UnsupportedEncodingException {
                init(key.getBytes(encoding));
        }
        private void init(byte[] key) throws GeneralSecurityException {
                sk = new SecretKeySpec(key, HMAC_MD5_NAME);
                mac = Mac.getInstance(HMAC_MD5_NAME);
                mac.init(sk);
        }
        public byte[] ComputeHash(byte[] data) {
                return mac.doFinal(data);
        }
        public byte[] ComputeHash(String data, String encoding) throws
UnsupportedEncodingException {
                return ComputeHash(data.getBytes(encoding));
        }

class B64 {
     public static String b64encode(byte[] b) throws MessagingException,
IOException  {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         OutputStream b64os = MimeUtility.encode(baos, "base64");
         b64os.write(b);
         b64os.close();
         return new String(baos.toByteArray());
      }
      public static byte[] b64decode(String s) throws
MessagingException, IOException  {
         ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
         InputStream b64is = MimeUtility.decode(bais, "Base64");
         byte[] tmp = new byte[s.length()];
         int n = b64is.read(tmp);
         byte[] res = new byte[n];
         System.arraycopy(tmp, 0, res, 0, n);
         return res;
      }

Thanks Arne for the detailed response.  And the others who set me
straight.  I understand the suggested changes, but am still getting
strange results on the Android side:  I've included the full example
projects I'm using for .NET and Android, using Arne's test case, which
should produce "QkY+XkbFsIqyaQp5jPuLjw==" on the Java side, but
doesn't.  I did not implement Arne's B64 class yet, but that does not
appear to be the problem.  The bytes returned from the computeHash
call do not match the bytes returned from the equivalent .NET call.
Anyone know what's going on with the Android project.  Why doesn't it
yield the correct result?

Aaron

Android Project
================
package org.example.hmacmd5;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        TextView tv = new TextView(this);
        try {
           tv.setText(encodeText(new byte[]{ 1, 2, 3, 4, 5, 6, 7,
8 }, "This is a test!", "UTF-8"));
        } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }
        setContentView(tv);
   }

   public String encodeText(byte[] key, String sSrc, String sEncoding)
throws Exception {
      HMACMD5 hmacMD5 = new HMACMD5(key);
      byte[] textBytes = EncodingUtils.getBytes(sSrc, sEncoding);
      byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
      String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
      return sEncodedText;
   }

}

package org.example.hmacmd5;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HMACMD5 {
   private final String HMAC_MD5_NAME = "HmacMD5";
   private SecretKeySpec sk;
   private Mac mac;

   public HMACMD5(byte[] key) throws GeneralSecurityException {
      init(key);
   }

   public HMACMD5(String key, String encoding) throws
GeneralSecurityException, UnsupportedEncodingException {
      init(key.getBytes(encoding));
   }

   private void init(byte[] key) throws GeneralSecurityException {
      sk = new SecretKeySpec(key, HMAC_MD5_NAME);
      mac = Mac.getInstance(HMAC_MD5_NAME);
      mac.init(sk);
   }

   public byte[] ComputeHash(byte[] data) {
      return mac.doFinal(data);
   }

   public byte[] ComputeHash(String data, String encoding) throws
UnsupportedEncodingException {
      return ComputeHash(data.getBytes(encoding));
   }

}

.NET code
===================
using System;
using System.Text;
using System.Security.Cryptography;

namespace HmacMD5Test
{
   class Program
   {
      public static string EncodeText(byte[] key, string sText,
Encoding encoding)
      {
         HMACMD5 hmacMD5 = new
...

read more »

Realized that Java has signed and unsigned bytes. This got me looking
at the Base64 encoding, which wasn't working. Found a working Android
Base64 class that fixed the problem. Thanks for all the help.

Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top